Module: YAML::BaseNode

Included in:
Syck::Node, YamlNode
Defined in:
lib/yaml/basenode.rb

Overview

YAML Generic Model container

Instance Method Summary collapse

Instance Method Details

#[](*key) ⇒ Object

We want the node to act like as Hash if it is.



185
186
187
188
189
190
191
192
# File 'lib/yaml/basenode.rb', line 185

def []( *key )
    if Hash === @value
        v = @value.detect { |k,| k.transform == key.first }
        v[1] if v
    elsif Array === @value
        @value.[]( *key )
    end
end

#at(seg) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/yaml/basenode.rb', line 69

def at( seg )
    if Hash === @value
        self[seg]
    elsif Array === @value and seg =~ /\A\d+\Z/ and @value[seg.to_i]
        @value[seg.to_i]
    end
end

#childrenObject



194
195
196
197
198
199
200
# File 'lib/yaml/basenode.rb', line 194

def children
    if Hash === @value
        @value.values.collect { |c| c[1] }
    elsif Array === @value
        @value
    end
end

#children_with_indexObject



202
203
204
205
206
207
208
# File 'lib/yaml/basenode.rb', line 202

def children_with_index
    if Hash === @value
        @value.keys.collect { |i| [self[i], i] }
    elsif Array === @value
        i = -1; @value.collect { |v| i += 1; [v, i] }
    end
end

#emitObject



210
211
212
# File 'lib/yaml/basenode.rb', line 210

def emit
    transform.to_yaml
end

#match_path(ypath_str) ⇒ Object

YPath search returning a complete depth array



80
81
82
83
84
85
86
87
88
# File 'lib/yaml/basenode.rb', line 80

def match_path( ypath_str )
    depth = 0
    matches = []
    YPath.each_path( ypath_str ) do |ypath|
        seg = match_segment( ypath, 0 )
        matches += seg if seg
    end
    matches.uniq
end

#match_segment(ypath, depth) ⇒ Object

Search a node for a single YPath segment



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/yaml/basenode.rb', line 93

def match_segment( ypath, depth )
    deep_nodes = []
    seg = ypath.segments[ depth ]
    if seg == "/"
        unless String === @value
            idx = -1
            @value.collect { |v|
                idx += 1
                if Hash === @value
                    match_init = [v[0].transform, v[1]]
                    match_deep = v[1].match_segment( ypath, depth )
                else
                    match_init = [idx, v]
                    match_deep = v.match_segment( ypath, depth )
                end
                if match_deep
                    match_deep.each { |m|
                        deep_nodes.push( match_init + m )
                    }
                end
            }
        end
        depth += 1
        seg = ypath.segments[ depth ]
    end
    match_nodes =
        case seg
        when "."
            [[nil, self]]
        when ".."
            [["..", nil]]
        when "*"
            if @value.is_a? Enumerable
                idx = -1
                @value.collect { |h|
                    idx += 1
                    if Hash === @value
                        [h[0].transform, h[1]]
                    else
                        [idx, h]
                    end
                }
            end
        else
            if seg =~ /^"(.*)"$/
                seg = $1
            elsif seg =~ /^'(.*)'$/
                seg = $1
            end
            if ( v = at( seg ) )
                [[ seg, v ]]
            end
        end
    return deep_nodes unless match_nodes
    pred = ypath.predicates[ depth ]
    if pred
        case pred
        when /^\.=/
            pred = $'   # '
            match_nodes.reject! { |n|
                n.last.value != pred
            }
        else
            match_nodes.reject! { |n|
                n.last.at( pred ).nil?
            }
        end
    end
    return match_nodes + deep_nodes unless ypath.segments.length > depth + 1

    #puts "DEPTH: #{depth + 1}"
    deep_nodes = []
    match_nodes.each { |n|
        if n[1].is_a? BaseNode
            match_deep = n[1].match_segment( ypath, depth + 1 )
            if match_deep
                match_deep.each { |m|
                    deep_nodes.push( n + m )
                }
            end
        else
            deep_nodes = []
        end
    }
    deep_nodes = nil if deep_nodes.length == 0
    deep_nodes
end

#search(ypath_str) ⇒ Object

Search for YPath entry and return a list of qualified paths.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/yaml/basenode.rb', line 55

def search( ypath_str )
    matches = match_path( ypath_str )

    if matches
        matches.collect { |m|
            path = []
            m.each_index { |i|
                path.push m[i] if ( i % 2 ).zero?
            }
            "/" + path.compact.join( "/" )
        }
    end
end

#select(ypath_str) ⇒ Object

Search for YPath entry and return qualified nodes.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/yaml/basenode.rb', line 17

def select( ypath_str )
    matches = match_path( ypath_str )

    #
    # Create a new generic view of the elements selected
    #
    if matches
        result = []
        matches.each { |m|
            result.push m.last
        }
        YAML.transfer( 'seq', result )
    end
end

#select!(ypath_str) ⇒ Object

Search for YPath entry and return transformed nodes.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/yaml/basenode.rb', line 36

def select!( ypath_str )
    matches = match_path( ypath_str )

    #
    # Create a new generic view of the elements selected
    #
    if matches
        result = []
        matches.each { |m|
            result.push m.last.transform
        }
        result
    end
end