diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2009-10-08 22:50:06 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2009-10-08 22:50:06 -0700 |
commit | 34b588f382e297ee80ddfef084923b4fc7bfc7b9 (patch) | |
tree | 39e1e0f8564533cdd39e900274ad876a241ef801 | |
parent | df10175978d74736fde61dca28d5129fa5b65b98 (diff) | |
download | psych-34b588f382e297ee80ddfef084923b4fc7bfc7b9.zip |
struct subclasses work
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 12 | ||||
-rw-r--r-- | lib/psych/visitors/yast_builder.rb | 4 | ||||
-rw-r--r-- | test/psych/test_serialize_subclasses.rb | 16 |
3 files changed, 30 insertions, 2 deletions
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index fec2614..66fefef 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -99,7 +99,7 @@ module Psych case o.tag when /!ruby\/struct:?(.*)?$/ klassname = $1 - h = Hash[*o.children.map { |c| accept c }].to_a + members = o.children.map { |c| accept c } if klassname && klassname.length > 1 name = klassname @@ -118,9 +118,17 @@ module Psych end raise ex end - h.each { |k,v| s.send("#{k}=", v) } + struct_members = s.members.map { |x| x.to_sym } + members.each_slice(2) { |k,v| + if struct_members.include? k.to_sym + s.send("#{k}=", v) + else + s.instance_variable_set(:"@#{k}", v) + end + } s else + h = Hash[*members] Struct.new(*h.map { |k,v| k.to_sym }).new(*h.map { |k,v| v }) end diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb index e8f9891..6c5f25f 100644 --- a/lib/psych/visitors/yast_builder.rb +++ b/lib/psych/visitors/yast_builder.rb @@ -41,6 +41,10 @@ module Psych accept member accept o[member] end + o.instance_variables.each do |iv| + accept iv.to_s.sub(/^@/, '') + accept o.instance_variable_get(iv) + end @stack.pop end diff --git a/test/psych/test_serialize_subclasses.rb b/test/psych/test_serialize_subclasses.rb index f500fb1..151b61a 100644 --- a/test/psych/test_serialize_subclasses.rb +++ b/test/psych/test_serialize_subclasses.rb @@ -19,5 +19,21 @@ module Psych so = SomeObject.new('foo', [1,2,3]) assert_equal so, Psych.load(Psych.dump(so)) end + + class StructSubclass < Struct.new(:foo) + def initialize foo, bar + super(foo) + @bar = bar + end + + def == other + super(other) && @bar == other.instance_eval{ @bar } + end + end + + def test_struct_subclass + so = StructSubclass.new('foo', [1,2,3]) + assert_equal so, Psych.load(Psych.dump(so)) + end end end |