Class: YAML::Transformer
Defined Under Namespace
Class Method Summary collapse
-
.traverse(path, tree, &block) ⇒ Object
Work like XLST.
Instance Method Summary collapse
-
#add(path, name = nil, &block) ⇒ Object
(also: #register)
Register the given block and compile the path.
-
#clear ⇒ Object
Clear compiled rules.
-
#dottify(filename) ⇒ Object
Print graph in a file in doty format.
-
#initialize ⇒ Transformer
constructor
Constructor.
-
#reset ⇒ Object
Reset position to root.
-
#traverse(tree) ⇒ Object
Traverse an entire YAML tree.
- #update_node(node, value, segment, remember_index, name, &block) ⇒ Object
Constructor Details
#initialize ⇒ Transformer
Constructor.
207 208 209 210 211 |
# File 'lib/yaml_extensions/transform.rb', line 207 def initialize @rootgraph = Root.new @graph = [] @activated = [] end |
Class Method Details
.traverse(path, tree, &block) ⇒ Object
Work like XLST. Traverse all tree and return a list of element that match the path.
288 289 290 291 292 |
# File 'lib/yaml_extensions/transform.rb', line 288 def self.traverse ( path, tree, &block ) p = self.new p.add(path, &block) p.traverse(tree) end |
Instance Method Details
#add(path, name = nil, &block) ⇒ Object Also known as: register
Register the given block and compile the path.
214 215 216 217 218 219 220 |
# File 'lib/yaml_extensions/transform.rb', line 214 def add ( path, name=nil, &block) raise ArgumentError unless (path.kind_of?(YRegexPath)) raise ArgumentError if (block.nil?) current_nodes = path.root ? @rootgraph.children : @graph add_segment(path.segments, path.wanted_node_index, current_nodes, name, &block) end |
#clear ⇒ Object
Clear compiled rules.
276 277 278 |
# File 'lib/yaml_extensions/transform.rb', line 276 def clear initialize end |
#dottify(filename) ⇒ Object
Print graph in a file in doty format
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/yaml_extensions/transform.rb', line 253 def dottify ( filename ) File.open(filename, 'w') do |f| f << <<EOF /* Compiled rules graph visualisation by YSLT */ digraph "Compiled rules" { node [style=filled] edge [style=solid, arrowtail=none] rankdir=LR EOF @rootgraph.dottify(f) @graph.each do |n| n.dottify(f) end f << "}\n" end end |
#reset ⇒ Object
Reset position to root.
271 272 273 |
# File 'lib/yaml_extensions/transform.rb', line 271 def reset @activated = @rootgraph.children + @graph end |
#traverse(tree) ⇒ Object
Traverse an entire YAML tree.
281 282 283 284 |
# File 'lib/yaml_extensions/transform.rb', line 281 def traverse ( tree ) reset tree.yaml_doc_traverse(@activated) end |
#update_node(node, value, segment, remember_index, name, &block) ⇒ Object
246 247 248 249 250 |
# File 'lib/yaml_extensions/transform.rb', line 246 def update_node ( node, value, segment, remember_index, name, &block ) sons = node.get_associated(value, remember_index == 1) add_segment(segment, remember_index - 1, sons, name, &block) return node end |