diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2009-09-30 17:12:14 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2009-09-30 17:12:14 -0700 |
commit | 69037516509239cc9f700265b1579ccd92eafead (patch) | |
tree | eaf06f745ea459697f7dfbc24cf956136c9e302e | |
parent | aeda4bb2ae215a67f862edb36f52f50358258278 (diff) | |
download | psych-69037516509239cc9f700265b1579ccd92eafead.zip |
ranges round trip
-rw-r--r-- | .autotest | 1 | ||||
-rw-r--r-- | lib/psych/ruby.rb | 9 | ||||
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 18 | ||||
-rw-r--r-- | lib/psych/visitors/yast_builder.rb | 8 | ||||
-rw-r--r-- | test/visitors/test_yast_builder.rb | 8 |
5 files changed, 37 insertions, 7 deletions
@@ -1,6 +1,7 @@ require "autotest/restart" Autotest.add_hook :initialize do |at| + at.find_directories = ARGV unless ARGV.empty? at.testlib = "minitest/autorun" end diff --git a/lib/psych/ruby.rb b/lib/psych/ruby.rb index 255f73c..1412da5 100644 --- a/lib/psych/ruby.rb +++ b/lib/psych/ruby.rb @@ -1,6 +1,13 @@ [ Object, String, Class, Hash, Array, NilClass, Float, - FalseClass, TrueClass + FalseClass, TrueClass, Range + # Struct + # Exception + # Regexp + # Time + # Date + # Rational + # Complex ].each do |klass| klass.send(:remove_method, :to_yaml) rescue NameError end diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index b05b34b..ef8a279 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -27,12 +27,18 @@ module Psych end def visit_Psych_Nodes_Mapping o - hash = {} - @st[o.anchor] = hash if o.anchor - o.children.map { |c| c.accept self }.each_slice(2) { |k,v| - hash[k] = v - } - hash + case o.tag + when 'ruby/range' + h = Hash[*o.children.map { |c| c.accept self }] + Range.new(h['begin'], h['end'], h['excl']) + else + hash = {} + @st[o.anchor] = hash if o.anchor + o.children.map { |c| c.accept self }.each_slice(2) { |k,v| + hash[k] = v + } + hash + end end def visit_Psych_Nodes_Document o diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb index 25cfe34..7b5d53b 100644 --- a/lib/psych/visitors/yast_builder.rb +++ b/lib/psych/visitors/yast_builder.rb @@ -54,6 +54,14 @@ module Psych raise TypeError, "can't dump anonymous class #{o.class}" end + def visit_Range o + @stack.push append Nodes::Mapping.new(nil, 'ruby/range', false) + ['begin', o.begin, 'end', o.end, 'excl', o.exclude_end?].each do |m| + m.accept self + end + @stack.pop + end + def visit_Hash o if node = @st[o.object_id] node.anchor = o.object_id.to_s diff --git a/test/visitors/test_yast_builder.rb b/test/visitors/test_yast_builder.rb index 9d2945b..ec74462 100644 --- a/test/visitors/test_yast_builder.rb +++ b/test/visitors/test_yast_builder.rb @@ -33,6 +33,14 @@ module Psych assert_round_trip 'false' end + def test_range_inclusive + assert_round_trip 1..2 + end + + def test_range_exclusive + assert_round_trip 1...2 + end + def test_binary string = [0, 123,22, 44, 9, 32, 34, 39].pack('C*') assert_equal string, Psych.load(string.to_yaml) |