summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2009-10-05 11:08:19 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2009-10-05 11:08:19 -0700
commit3027b4d69c2fe2e3f4f254e102af3834fddff2f6 (patch)
tree5125565c50a2e63f1819ddaae1798aeaf4618057
parent81929a6aa2b6d26958401e67b83019e31275ef5c (diff)
downloadpsych-3027b4d69c2fe2e3f4f254e102af3834fddff2f6.zip
rationals supported
-rw-r--r--lib/psych/ruby.rb3
-rw-r--r--lib/psych/visitors/to_ruby.rb6
-rw-r--r--lib/psych/visitors/yast_builder.rb8
-rw-r--r--test/visitors/test_to_ruby.rb10
-rw-r--r--test/visitors/test_yast_builder.rb4
5 files changed, 29 insertions, 2 deletions
diff --git a/lib/psych/ruby.rb b/lib/psych/ruby.rb
index d4d4e66..ae5dbce 100644
--- a/lib/psych/ruby.rb
+++ b/lib/psych/ruby.rb
@@ -2,13 +2,12 @@ require 'complex'
[
Object, String, Class, Hash, Array, NilClass, Float,
- FalseClass, TrueClass, Range, Complex
+ FalseClass, TrueClass, Range, Complex, Rational
# Struct
# Exception
# Regexp
# Time
# Date
- # Rational
].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 54e02d6..757e27d 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/lib/psych/visitors/to_ruby.rb
@@ -31,9 +31,15 @@ module Psych
when 'ruby/range'
h = Hash[*o.children.map { |c| accept c }]
Range.new(h['begin'], h['end'], h['excl'])
+
when 'ruby/object:Complex'
h = Hash[*o.children.map { |c| accept c }]
Complex(h['real'], h['image'])
+
+ when 'ruby/object:Rational'
+ h = Hash[*o.children.map { |c| accept c }]
+ Rational(h['numerator'], h['denominator'])
+
else
hash = {}
@st[o.anchor] = hash if o.anchor
diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb
index f6998d2..a886af4 100644
--- a/lib/psych/visitors/yast_builder.rb
+++ b/lib/psych/visitors/yast_builder.rb
@@ -21,6 +21,14 @@ module Psych
raise TypeError, "Can't dump #{target.class}"
end
+ def visit_Rational o
+ @stack.push append Nodes::Mapping.new(nil,'ruby/object:Rational',false)
+ ['denominator', o.denominator, 'numerator', o.numerator].each do |m|
+ accept m
+ end
+ @stack.pop
+ end
+
def visit_Complex o
@stack.push append Nodes::Mapping.new(nil, 'ruby/object:Complex', false)
['real', o.real, 'image', o.image].each do |m|
diff --git a/test/visitors/test_to_ruby.rb b/test/visitors/test_to_ruby.rb
index 39a4567..7265316 100644
--- a/test/visitors/test_to_ruby.rb
+++ b/test/visitors/test_to_ruby.rb
@@ -9,6 +9,16 @@ module Psych
@visitor = ToRuby.new
end
+ def test_rational
+ mapping = Nodes::Mapping.new nil, 'ruby/object:Rational'
+ mapping.children << Nodes::Scalar.new('denominator')
+ mapping.children << Nodes::Scalar.new('2')
+ mapping.children << Nodes::Scalar.new('numerator')
+ mapping.children << Nodes::Scalar.new('1')
+
+ assert_equal Rational(1,2), mapping.to_ruby
+ end
+
def test_complex
mapping = Nodes::Mapping.new nil, 'ruby/object:Complex'
mapping.children << Nodes::Scalar.new('image')
diff --git a/test/visitors/test_yast_builder.rb b/test/visitors/test_yast_builder.rb
index 6681575..f5423e9 100644
--- a/test/visitors/test_yast_builder.rb
+++ b/test/visitors/test_yast_builder.rb
@@ -8,6 +8,10 @@ module Psych
@v = Visitors::YASTBuilder.new
end
+ def test_rational
+ assert_round_trip Rational(1,2)
+ end
+
def test_complex
assert_round_trip Complex(1,2)
end