class Mixml::Tool

Mixml tool

This is the main class for using mixml.

Attributes

documents[R]

@return [Array<Document>] loaded XML Documents

indent[RW]

@return [Integer] indent width, defaults to 4

pretty[RW]

@return [Boolean] pretty print XML during output

print[RW]

@return [Boolean] print processed XML documents, defaults to true

save[RW]

@return [Boolean] save processed XML documents, defaults to false

Public Class Methods

new() click to toggle source

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

css(*selectors, &block) click to toggle source

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(program = nil, &block) click to toggle source

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
flush() click to toggle source

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(*file_names) click to toggle source

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() { |document, options| ... } click to toggle source

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() click to toggle source

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]

process() { |xml| ... } click to toggle source

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() click to toggle source

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() click to toggle source

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
template(text) click to toggle source

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
with_nodes(selection) { |node| ... } click to toggle source

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
with_nodesets(selection) { |nodeset| ... } click to toggle source

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
work(*file_names, &block) click to toggle source

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
xml(proc) click to toggle source

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
xpath(*paths, &block) click to toggle source

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