Class: Mixml::Tool
- Inherits:
-
Object
- Object
- Mixml::Tool
- Defined in:
- lib/mixml/tool.rb
Overview
Mixml tool
This is the main class for using mixml.
Instance Attribute Summary collapse
-
#documents ⇒ Array<Document>
readonly
Loaded XML Documents.
-
#indent ⇒ Integer
Indent width, defaults to 4.
-
#pretty ⇒ Boolean
Pretty print XML during output.
-
#print ⇒ Boolean
Print processed XML documents, defaults to true.
-
#save ⇒ Boolean
Save processed XML documents, defaults to false.
Instance Method Summary collapse
-
#css(*selectors) { ... } ⇒ void
Select nodes using CSS selectors and execute DSL commands for these nodes.
-
#execute(program = nil) { ... } ⇒ void
Execute a script or a block.
-
#flush ⇒ void
Print/save all loaded XML files and then remove them.
-
#initialize ⇒ Tool
constructor
Intialize a new mixml tool.
-
#load(*file_names) ⇒ void
Load XML files.
-
#output_all {|document| ... } ⇒ Object
Output all loaded XML files.
-
#print_all ⇒ void
Print all loaded XML files.
-
#process {|xml| ... } ⇒ void
Execute a block for each loaded XML document.
-
#remove_all ⇒ void
Remove all loaded XML files.
-
#save_all ⇒ void
Save all loaded XML files.
-
#template(text) ⇒ Template
Create a DSL replacement template.
-
#with_nodes(selection) {|node| ... } ⇒ Object
Execute block for each node.
-
#with_nodesets(selection) {|node| ... } ⇒ Object
Execute block for each node set.
-
#work(*file_names) { ... } ⇒ void
Perform work on a list of XML files.
-
#xml(proc) ⇒ Template
Create a XML replacement template.
-
#xpath(*paths) { ... } ⇒ void
Select nodes using an XPath expression and execute DSL commands for these nodes.
Constructor Details
#initialize ⇒ Tool
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
#documents ⇒ Array<Document> (readonly)
Returns loaded XML Documents.
16 17 18 |
# File 'lib/mixml/tool.rb', line 16 def documents @documents end |
#indent ⇒ Integer
Returns indent width, defaults to 4.
28 29 30 |
# File 'lib/mixml/tool.rb', line 28 def indent @indent end |
#pretty ⇒ Boolean
Returns pretty print XML during output.
19 20 21 |
# File 'lib/mixml/tool.rb', line 19 def pretty @pretty end |
#print ⇒ Boolean
Returns print processed XML documents, defaults to true.
25 26 27 |
# File 'lib/mixml/tool.rb', line 25 def print @print end |
#save ⇒ Boolean
Returns 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
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
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 |
#flush ⇒ void
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
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
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/mixml/tool.rb', line 60 def output_all = {} if @pretty then [:indent] = @indent end @documents.each do |document| yield document, end end |
#print_all ⇒ void
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, | if @documents.size > 1 then puts '-' * document.name.length puts document.name puts '-' * document.name.length end puts document.xml.to_xml() end end |
#process {|xml| ... } ⇒ void
This method returns an undefined value.
Execute a block for each loaded 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_all ⇒ void
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_all ⇒ void
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, | File.open(document.name, 'w') do |file| document.xml.write_xml_to(file, ) end end end |
#template(text) ⇒ Template
Create a DSL replacement template
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
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
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
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
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
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 |