Line | |
---|
1 | #
|
---|
2 | # YAML::YPath
|
---|
3 | #
|
---|
4 |
|
---|
5 | module YAML
|
---|
6 |
|
---|
7 | class YPath
|
---|
8 | attr_accessor :segments, :predicates, :flags
|
---|
9 | def initialize( str )
|
---|
10 | @segments = []
|
---|
11 | @predicates = []
|
---|
12 | @flags = nil
|
---|
13 | while str =~ /^\/?(\/|[^\/\[]+)(?:\[([^\]]+)\])?/
|
---|
14 | @segments.push $1
|
---|
15 | @predicates.push $2
|
---|
16 | str = $'
|
---|
17 | end
|
---|
18 | unless str.to_s.empty?
|
---|
19 | @segments += str.split( "/" )
|
---|
20 | end
|
---|
21 | if @segments.length == 0
|
---|
22 | @segments.push "."
|
---|
23 | end
|
---|
24 | end
|
---|
25 | def YPath.each_path( str )
|
---|
26 | #
|
---|
27 | # Find choices
|
---|
28 | #
|
---|
29 | paths = []
|
---|
30 | str = "(#{ str })"
|
---|
31 | while str.sub!( /\(([^()]+)\)/, "\n#{ paths.length }\n" )
|
---|
32 | paths.push $1.split( '|' )
|
---|
33 | end
|
---|
34 |
|
---|
35 | #
|
---|
36 | # Construct all possible paths
|
---|
37 | #
|
---|
38 | all = [ str ]
|
---|
39 | ( paths.length - 1 ).downto( 0 ) do |i|
|
---|
40 | all = all.collect do |a|
|
---|
41 | paths[i].collect do |p|
|
---|
42 | a.gsub( /\n#{ i }\n/, p )
|
---|
43 | end
|
---|
44 | end.flatten.uniq
|
---|
45 | end
|
---|
46 | all.collect do |path|
|
---|
47 | yield YPath.new( path )
|
---|
48 | end
|
---|
49 | end
|
---|
50 | end
|
---|
51 |
|
---|
52 | end
|
---|
Note:
See
TracBrowser
for help on using the repository browser.