summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/psych/ruby.rb5
-rw-r--r--lib/psych/visitors/emitter.rb8
-rw-r--r--lib/psych/visitors/to_ruby.rb13
-rw-r--r--lib/psych/visitors/yast_builder.rb16
-rw-r--r--test/visitors/test_to_ruby.rb11
-rw-r--r--test/visitors/test_yast_builder.rb4
6 files changed, 42 insertions, 15 deletions
diff --git a/lib/psych/ruby.rb b/lib/psych/ruby.rb
index 1412da5..d4d4e66 100644
--- a/lib/psych/ruby.rb
+++ b/lib/psych/ruby.rb
@@ -1,13 +1,14 @@
+require 'complex'
+
[
Object, String, Class, Hash, Array, NilClass, Float,
- FalseClass, TrueClass, Range
+ FalseClass, TrueClass, Range, Complex
# 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/emitter.rb b/lib/psych/visitors/emitter.rb
index 45ebef5..0768fbb 100644
--- a/lib/psych/visitors/emitter.rb
+++ b/lib/psych/visitors/emitter.rb
@@ -7,13 +7,13 @@ module Psych
def visit_Psych_Nodes_Stream o
@handler.start_stream o.encoding
- o.children.each { |c| c.accept self }
+ o.children.each { |c| accept c }
@handler.end_stream
end
def visit_Psych_Nodes_Document o
@handler.start_document o.version, o.tag_directives, o.implicit
- o.children.each { |c| c.accept self }
+ o.children.each { |c| accept c }
@handler.end_document o.implicit_end
end
@@ -23,13 +23,13 @@ module Psych
def visit_Psych_Nodes_Sequence o
@handler.start_sequence o.anchor, o.tag, o.implicit, o.style
- o.children.each { |c| c.accept self }
+ o.children.each { |c| accept c }
@handler.end_sequence
end
def visit_Psych_Nodes_Mapping o
@handler.start_mapping o.anchor, o.tag, o.implicit, o.style
- o.children.each { |c| c.accept self }
+ o.children.each { |c| accept c }
@handler.end_mapping
end
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
index ef8a279..54e02d6 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/lib/psych/visitors/to_ruby.rb
@@ -22,19 +22,22 @@ module Psych
def visit_Psych_Nodes_Sequence o
list = []
@st[o.anchor] = list if o.anchor
- o.children.each { |c| list.push c.accept self }
+ o.children.each { |c| list.push accept c }
list
end
def visit_Psych_Nodes_Mapping o
case o.tag
when 'ruby/range'
- h = Hash[*o.children.map { |c| c.accept self }]
+ 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'])
else
hash = {}
@st[o.anchor] = hash if o.anchor
- o.children.map { |c| c.accept self }.each_slice(2) { |k,v|
+ o.children.map { |c| accept c }.each_slice(2) { |k,v|
hash[k] = v
}
hash
@@ -42,11 +45,11 @@ module Psych
end
def visit_Psych_Nodes_Document o
- o.root.accept self
+ accept o.root
end
def visit_Psych_Nodes_Stream o
- o.children.map { |c| c.accept self }
+ o.children.map { |c| accept c }
end
def visit_Psych_Nodes_Alias o
diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb
index 7b5d53b..f6998d2 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_Complex o
+ @stack.push append Nodes::Mapping.new(nil, 'ruby/object:Complex', false)
+ ['real', o.real, 'image', o.image].each do |m|
+ accept m
+ end
+ @stack.pop
+ end
+
def visit_Integer o
append Nodes::Scalar.new o.to_s
end
@@ -57,7 +65,7 @@ module Psych
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
+ accept m
end
@stack.pop
end
@@ -74,8 +82,8 @@ module Psych
@stack.push append map
o.each do |k,v|
- k.accept self
- v.accept self
+ accept k
+ accept v
end
@stack.pop
@@ -91,7 +99,7 @@ module Psych
@st[o.object_id] = seq
@stack.push append seq
- o.each { |c| c.accept self }
+ o.each { |c| accept c }
@stack.pop
end
diff --git a/test/visitors/test_to_ruby.rb b/test/visitors/test_to_ruby.rb
index c252573..39a4567 100644
--- a/test/visitors/test_to_ruby.rb
+++ b/test/visitors/test_to_ruby.rb
@@ -1,5 +1,6 @@
require 'minitest/autorun'
require 'psych'
+require 'complex'
module Psych
module Visitors
@@ -8,6 +9,16 @@ module Psych
@visitor = ToRuby.new
end
+ def test_complex
+ mapping = Nodes::Mapping.new nil, 'ruby/object:Complex'
+ mapping.children << Nodes::Scalar.new('image')
+ mapping.children << Nodes::Scalar.new('2')
+ mapping.children << Nodes::Scalar.new('real')
+ mapping.children << Nodes::Scalar.new('1')
+
+ assert_equal Complex(1,2), mapping.to_ruby
+ end
+
def test_integer
i = Nodes::Scalar.new('1', nil, 'tag:yaml.org,2002:int')
assert_equal 1, i.to_ruby
diff --git a/test/visitors/test_yast_builder.rb b/test/visitors/test_yast_builder.rb
index d00a21c..6681575 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_complex
+ assert_round_trip Complex(1,2)
+ end
+
def test_circular_list
a = []
2.times { a << a }