9
# YAML Generic Model container
14
# Search for YPath entry and return
17
def select( ypath_str )
18
matches = match_path( ypath_str )
21
# Create a new generic view of the elements selected
28
YAML.transfer( 'seq', result )
33
# Search for YPath entry and return
36
def select!( ypath_str )
37
matches = match_path( ypath_str )
40
# Create a new generic view of the elements selected
45
result.push m.last.transform
52
# Search for YPath entry and return a list of
55
def search( ypath_str )
56
matches = match_path( ypath_str )
62
path.push m[i] if ( i % 2 ).zero?
64
"/" + path.compact.join( "/" )
72
elsif Array === @value and seg =~ /\A\d+\Z/ and @value[seg.to_i]
78
# YPath search returning a complete depth array
80
def match_path( ypath_str )
83
YPath.each_path( ypath_str ) do |ypath|
84
seg = match_segment( ypath, 0 )
91
# Search a node for a single YPath segment
93
def match_segment( ypath, depth )
95
seg = ypath.segments[ depth ]
97
unless String === @value
102
match_init = [v[0].transform, v[1]]
103
match_deep = v[1].match_segment( ypath, depth )
105
match_init = [idx, v]
106
match_deep = v.match_segment( ypath, depth )
109
match_deep.each { |m|
110
deep_nodes.push( match_init + m )
116
seg = ypath.segments[ depth ]
125
if @value.is_a? Enumerable
130
[h[0].transform, h[1]]
139
elsif seg =~ /^'(.*)'$/
146
return deep_nodes unless match_nodes
147
pred = ypath.predicates[ depth ]
152
match_nodes.reject! { |n|
156
match_nodes.reject! { |n|
157
n.last.at( pred ).nil?
161
return match_nodes + deep_nodes unless ypath.segments.length > depth + 1
163
#puts "DEPTH: #{depth + 1}"
165
match_nodes.each { |n|
166
if n[1].is_a? BaseNode
167
match_deep = n[1].match_segment( ypath, depth + 1 )
169
match_deep.each { |m|
170
deep_nodes.push( n + m )
177
deep_nodes = nil if deep_nodes.length == 0
182
# We want the node to act like as Hash
187
v = @value.detect { |k,v| k.transform == key.first }
189
elsif Array === @value
196
@value.values.collect { |c| c[1] }
197
elsif Array === @value
202
def children_with_index
204
@value.keys.collect { |i| [self[i], i] }
205
elsif Array === @value
206
i = -1; @value.collect { |v| i += 1; [v, i] }