diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2009-10-06 16:42:39 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2009-10-06 16:42:39 -0700 |
commit | 4d48bff3590440b1bc35daf511efb216258b87d0 (patch) | |
tree | 52b854d771b70f80b929d34cefa34ca72c543276 | |
parent | efa7a36b3c151bf958b92ebb714a2c98fe4d5ebe (diff) | |
download | psych-4d48bff3590440b1bc35daf511efb216258b87d0.zip |
string ranges supported
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 32 | ||||
-rw-r--r-- | test/visitors/test_to_ruby.rb | 10 |
2 files changed, 32 insertions, 10 deletions
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index 6aea934..abdfd4c 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -13,19 +13,31 @@ module Psych def visit_Psych_Nodes_Scalar o @st[o.anchor] = o.value if o.anchor - return o.value if ['!str', 'tag:yaml.org,2002:str'].include?(o.tag) - return Complex(o.value) if o.tag == "!ruby/object:Complex" - return Rational(o.value) if o.tag == "!ruby/object:Rational" return o.value if o.quoted - token = ScalarScanner.new(o.value).tokenize - - case token.first - when :DATE - require 'date' - Date.strptime token.last, '%Y-%m-%d' + case o.tag + when '!str', 'tag:yaml.org,2002:str' + o.value + when "!ruby/object:Complex" + Complex(o.value) + when "!ruby/object:Rational" + Rational(o.value) + when "!ruby/range" + args = o.value.split(/([.]{2,3})/, 2).map { |s| + accept Nodes::Scalar.new(s) + } + args.push(args.delete_at(1) == '...') + Range.new(*args) else - token.last + token = ScalarScanner.new(o.value).tokenize + + case token.first + when :DATE + require 'date' + Date.strptime token.last, '%Y-%m-%d' + else + token.last + end end end diff --git a/test/visitors/test_to_ruby.rb b/test/visitors/test_to_ruby.rb index 63a5006..c60a9b9 100644 --- a/test/visitors/test_to_ruby.rb +++ b/test/visitors/test_to_ruby.rb @@ -52,6 +52,16 @@ module Psych end end + def test_range_string + node = Nodes::Scalar.new '1..2', nil, "!ruby/range" + assert_equal 1..2, node.to_ruby + end + + def test_range_string_triple + node = Nodes::Scalar.new '1...3', nil, "!ruby/range" + assert_equal 1...3, node.to_ruby + end + def test_integer i = Nodes::Scalar.new('1', nil, 'tag:yaml.org,2002:int') assert_equal 1, i.to_ruby |