Class: YAML::Transformer

Inherits:
Object show all
Defined in:
lib/yaml_extensions/transform.rb

Defined Under Namespace

Classes: Match, Node, Root

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransformer

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.

Raises:

  • (ArgumentError)


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

#clearObject

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

#resetObject

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