Class: Mixml::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/mixml/tool.rb

Overview

Mixml tool

This is the main class for using mixml.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTool

Intialize a new mixml tool



31
32
33
34
35
36
37
# File 'lib/mixml/tool.rb', line 31

def initialize
    @indent = 4
    @pretty = false
    @save = false
    @print = true
    @documents = []
end

Instance Attribute Details

#documentsArray<Document> (readonly)

Returns loaded XML Documents.

Returns:

  • (Array<Document>)

    loaded XML Documents



16
17
18
# File 'lib/mixml/tool.rb', line 16

def documents
  @documents
end

#indentInteger

Returns indent width, defaults to 4.

Returns:

  • (Integer)

    indent width, defaults to 4



28
29
30
# File 'lib/mixml/tool.rb', line 28

def indent
  @indent
end

#prettyBoolean

Returns pretty print XML during output.

Returns:

  • (Boolean)

    pretty print XML during output



19
20
21
# File 'lib/mixml/tool.rb', line 19

def pretty
  @pretty
end

Returns print processed XML documents, defaults to true.

Returns:

  • (Boolean)

    print processed XML documents, defaults to true



25
26
27
# File 'lib/mixml/tool.rb', line 25

def print
  @print
end

#saveBoolean

Returns save processed XML documents, defaults to false.

Returns:

  • (Boolean)

    save processed XML documents, defaults to false



22
23
24
# File 'lib/mixml/tool.rb', line 22

def save
  @save
end

Instance Method Details

#css(*selectors) { ... } ⇒ void

This method returns an undefined value.

Select nodes using CSS selectors and execute DSL commands for these nodes

Parameters:

  • selectors (Array<String>)

    CSS selectors

Yields:

  • Block to execute for each nodeset



189
190
191
192
193
194
195
196
197
198
199
200
201
# 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) { ... } ⇒ void

This method returns an undefined value.

Execute a script or a block

Parameters:

  • program (String) (defaults to: nil)

    DSL script to execute

Yields:

  • Block to execute



224
225
226
227
228
229
230
231
232
# 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

#flushvoid

This method returns an undefined value.

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.



116
117
118
119
120
121
122
123
124
125
126
# 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) ⇒ void

This method returns an undefined value.

Load XML files

Parameters:

  • file_names (Array)

    Names of the XML files to load



43
44
45
46
47
48
49
50
51
52
53
54
# 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| ... } ⇒ Object

Output all loaded XML files

Yields:

  • Block to write each document

Yield Parameters:

  • document (Document)

    Document to write



60
61
62
63
64
65
66
67
68
69
70
# 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

This method returns an undefined value.

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.



91
92
93
94
95
96
97
98
99
100
# 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

#process {|xml| ... } ⇒ void

This method returns an undefined value.

Execute a block for each loaded XML document

Yields:

  • Block to execute for each XML document

Yield Parameters:

  • xml (Nokogiri::XML::Document)

    XML document



159
160
161
162
163
# File 'lib/mixml/tool.rb', line 159

def process
    @documents.each do |document|
        yield document.xml
    end
end

#remove_allvoid

This method returns an undefined value.

Remove all loaded XML files

Files are not saved before removing them.



107
108
109
# File 'lib/mixml/tool.rb', line 107

def remove_all
    @documents = []
end

#save_allvoid

This method returns an undefined value.

Save all loaded XML files

Pretty prints the XML if #pretty is enabled.



77
78
79
80
81
82
83
# 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) ⇒ Template

Create a DSL replacement template

Parameters:

  • text (String)

    Template text

Returns:



207
208
209
# File 'lib/mixml/tool.rb', line 207

def template(text)
    Template::Expression.new(text)
end

#with_nodes(selection) {|node| ... } ⇒ Object

Execute block for each node

Parameters:

Yields:

  • Block to execute for each node

Yield Parameters:

  • node (Nokogiri::XML::Node)

    Current node



239
240
241
242
243
244
245
# 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) {|node| ... } ⇒ Object

Execute block for each node set

Parameters:

Yields:

  • Block to execute for each node set

Yield Parameters:

  • node (Nokogiri::XML::NodeSet)

    Current node set



252
253
254
255
256
# File 'lib/mixml/tool.rb', line 252

def with_nodesets(selection, &block)
    selection.nodesets.each do |nodeset|
        yield nodeset
    end
end

#work(*file_names) { ... } ⇒ void

This method returns an undefined value.

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

Parameters:

  • file_names (Array)

    Names of the XML files to load

Yields:

  • Block to execute with loaded XML files



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 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) ⇒ Template

Create a XML replacement template

Parameters:

  • proc (Proc)

    Lambda to create XML

Returns:



215
216
217
# File 'lib/mixml/tool.rb', line 215

def xml(proc)
    Template::Xml.new(proc)
end

#xpath(*paths) { ... } ⇒ void

This method returns an undefined value.

Select nodes using an XPath expression and execute DSL commands for these nodes

Parameters:

  • paths (Array<String>)

    XPath expression

Yields:

  • Block to execute for each nodeset



170
171
172
173
174
175
176
177
178
179
180
181
182
# 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