diff options
author | why <why@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-05-09 21:25:50 +0000 |
---|---|---|
committer | why <why@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-05-09 21:25:50 +0000 |
commit | 55f4dc4c9a5345c28d0da750d1ee00fbb0870885 (patch) | |
tree | 904359659b75882365348decb2ca0789ca1ac803 /lib/yaml/ypath.rb | |
parent | 605adb86e2c2889c13e07cadf328f8032eebae7c (diff) | |
download | ruby-55f4dc4c9a5345c28d0da750d1ee00fbb0870885.tar.gz |
Initial checkin of YAML substances.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3772 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/yaml/ypath.rb')
-rw-r--r-- | lib/yaml/ypath.rb | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/yaml/ypath.rb b/lib/yaml/ypath.rb new file mode 100644 index 0000000000..713c4de76e --- /dev/null +++ b/lib/yaml/ypath.rb @@ -0,0 +1,52 @@ +# +# YAML::YPath +# + +module YAML + + class YPath + attr_accessor :segments, :predicates, :flags + def initialize( str ) + @segments = [] + @predicates = [] + @flags = nil + while str =~ /^\/?(\/|[^\/[]+)(?:\[([^\]]+)\])?/ + @segments.push $1 + @predicates.push $2 + str = $' + end + unless str.to_s.empty? + @segments += str.split( "/" ) + end + if @segments.length == 0 + @segments.push "." + end + end + def YPath.each_path( str ) + # + # Find choices + # + paths = [] + str = "(#{ str })" + while str.sub!( /\(([^()]+)\)/, "\n#{ paths.length }\n" ) + paths.push $1.split( '|' ) + end + + # + # Construct all possible paths + # + all = [ str ] + ( paths.length - 1 ).downto( 0 ) do |i| + all = all.collect do |a| + paths[i].collect do |p| + a.gsub( /\n#{ i }\n/, p ) + end + end.flatten.uniq + end + all.collect do |path| + yield YPath.new( path ) + end + end + end + +end |