class Mixml::Tool
Mixml
tool
This is the main class for using mixml.
Attributes
@return [Array<Document>] loaded XML Documents
@return [Integer] indent width, defaults to 4
@return [Boolean] pretty print XML during output
@return [Boolean] print processed XML documents, defaults to true
@return [Boolean] save processed XML documents, defaults to false
Public Class Methods
Intialize a new mixml tool
# File lib/mixml/tool.rb, line 31 def initialize @indent = 4 @pretty = false @save = false @print = true @documents = [] end
Public Instance Methods
Select nodes using CSS selectors and execute DSL commands for these nodes
@param selectors [Array<String>] CSS selectors @yield Block to execute for each nodeset @return [void]
# File lib/mixml/tool.rb, line 189 def css(*selectors, &block) nodesets = [] process do |xml| nodesets << xml.css(*selectors) end selection = Selection.new(nodesets) if block_given? then Docile.dsl_eval(selection, &block) end selection end
Execute a script or a block
@param program [String] DSL script to execute @yield Block to execute @return [void]
# File lib/mixml/tool.rb, line 224 def execute(program = nil, &block) if not program.nil? then instance_eval(program) end if not block.nil? then Docile.dsl_eval(self, &block) end end
Print/save all loaded XML files and then remove them
Files are saved if {#save} is enabled, and they are printed to the console if {#print} is enabled.
@return [void]
# File lib/mixml/tool.rb, line 116 def flush if @print then print_all end if @save then save_all end remove_all end
Load XML files
@param file_names [Array] Names of the XML files to load @return [void]
# File lib/mixml/tool.rb, line 43 def load(*file_names) file_names.flatten.each do |file_name| xml = File.open(file_name, 'r') do |file| Nokogiri::XML(file) do |config| if @pretty then config.default_xml.noblanks end end end @documents << Document.new(file_name, xml) end end
Output all loaded XML files
@yield Block to write each document @yieldparam document [Document] Document
to write
# File lib/mixml/tool.rb, line 60 def output_all options = {} if @pretty then options[:indent] = @indent end @documents.each do |document| yield document, options end end
Print all loaded XML files
Pretty prints the XML if {#pretty} is enabled. If more than one file is loaded, a header with the file’s name is printed before each file.
@return [void]
# File lib/mixml/tool.rb, line 91 def print_all output_all do |document, options| if @documents.size > 1 then puts '-' * document.name.length puts document.name puts '-' * document.name.length end puts document.xml.to_xml(options) end end
Execute a block for each loaded XML document
@yield Block to execute for each XML document @yieldparam xml {Nokogiri::XML::Document} XML document @return [void]
# File lib/mixml/tool.rb, line 159 def process @documents.each do |document| yield document.xml end end
Remove all loaded XML files
Files are not saved before removing them.
@return [void]
# File lib/mixml/tool.rb, line 107 def remove_all @documents = [] end
Save all loaded XML files
Pretty prints the XML if {#pretty} is enabled.
@return [void]
# File lib/mixml/tool.rb, line 77 def save_all output_all do |document, options| File.open(document.name, 'w') do |file| document.xml.write_xml_to(file, options) end end end
Create a DSL replacement template
@param text [String] Template
text @return [Template] Replacement template
# File lib/mixml/tool.rb, line 207 def template(text) Template::Expression.new(text) end
Execute block for each node
@param selection [Selection] Selected nodes @yield Block to execute for each node @yieldparam node [Nokogiri::XML::Node] Current node
# File lib/mixml/tool.rb, line 239 def with_nodes(selection) selection.nodesets.each do |nodeset| nodeset.each do |node| yield node end end end
Execute block for each node set
@param selection [Selection] Selected nodes @yield Block to execute for each node set @yieldparam node [Nokogiri::XML::NodeSet] Current node set
# File lib/mixml/tool.rb, line 252 def with_nodesets(selection, &block) selection.nodesets.each do |nodeset| yield nodeset end end
Perform work on a list of XML files
Perform the following steps: #. Remove all loaded XML files without saving them #. Load the supplied XML files #. Execute the supplied block #. Flush all XML files
@param file_names [Array] Names of the XML files to load @yield Block to execute with loaded XML files @return [void]
# File lib/mixml/tool.rb, line 139 def work(*file_names, &block) remove_all file_names.each do |file_name| load(file_name) if not block.nil? then execute(&block) end flush remove_all end end
Create a XML replacement template
@return [Template] Replacement template @param proc [Proc] Lambda to create XML
# File lib/mixml/tool.rb, line 215 def xml(proc) Template::Xml.new(proc) end
Select nodes using an XPath expression and execute DSL commands for these nodes
@param paths [Array<String>] XPath expression @yield Block to execute for each nodeset @return [void]
# File lib/mixml/tool.rb, line 170 def xpath(*paths, &block) nodesets = [] process do |xml| nodesets << xml.xpath(*paths) end selection = Selection.new(nodesets) if block_given? then Docile.dsl_eval(selection, &block) end selection end