* ext/psych/lib/psych/visitors/to_ruby.rb: revive hashes with ivars

* ext/psych/lib/psych/visitors/yaml_tree.rb: dump hashes with ivars.
  Fixes github.com/psych/issues/43

* test/psych/test_hash.rb: test for change

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49188 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
tenderlove 2015-01-08 22:00:54 +00:00
Родитель 2b2f9b75fe
Коммит 8c08c8298a
4 изменённых файлов: 76 добавлений и 8 удалений

Просмотреть файл

@ -1,3 +1,12 @@
Fri Jan 9 06:58:43 2015 Aaron Patterson <aaron@tenderlovemaking.com>
* ext/psych/lib/psych/visitors/to_ruby.rb: revive hashes with ivars
* ext/psych/lib/psych/visitors/yaml_tree.rb: dump hashes with ivars.
Fixes github.com/psych/issues/43
* test/psych/test_hash.rb: test for change
Thu Jan 8 17:05:00 2015 Seiei Higa <hanachin@gmail.com>
* vm_method.c (rb_method_entry): if no super class, no original

Просмотреть файл

@ -261,6 +261,20 @@ module Psych
end
set
when /^!ruby\/hash-with-ivars(?::(.*))?$/
hash = $1 ? resolve_class($1).new : {}
o.children.each_slice(2) do |key, value|
case key.value
when 'elements'
revive_hash hash, value
when 'ivars'
value.children.each_slice(2) do |k,v|
hash.instance_variable_set accept(k), accept(v)
end
end
end
hash
when /^!map:(.*)$/, /^!ruby\/hash:(.*)$/
revive_hash register(o, resolve_class($1).new), o

Просмотреть файл

@ -367,17 +367,46 @@ module Psych
end
def visit_Hash o
tag = o.class == ::Hash ? nil : "!ruby/hash:#{o.class}"
implicit = !tag
ivars = o.instance_variables
register(o, @emitter.start_mapping(nil, tag, implicit, Psych::Nodes::Mapping::BLOCK))
if ivars.any?
tag = "!ruby/hash-with-ivars"
tag << ":#{o.class}" unless o.class == ::Hash
o.each do |k,v|
accept k
accept v
register(o, @emitter.start_mapping(nil, tag, false, Psych::Nodes::Mapping::BLOCK))
@emitter.scalar 'elements', nil, nil, true, false, Nodes::Scalar::ANY
@emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
o.each do |k,v|
accept k
accept v
end
@emitter.end_mapping
@emitter.scalar 'ivars', nil, nil, true, false, Nodes::Scalar::ANY
@emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
o.instance_variables.each do |ivar|
accept ivar
accept o.instance_variable_get ivar
end
@emitter.end_mapping
@emitter.end_mapping
else
tag = o.class == ::Hash ? nil : "!ruby/hash:#{o.class}"
implicit = !tag
register(o, @emitter.start_mapping(nil, tag, implicit, Psych::Nodes::Mapping::BLOCK))
o.each do |k,v|
accept k
accept v
end
@emitter.end_mapping
end
@emitter.end_mapping
end
def visit_Psych_Set o

Просмотреть файл

@ -10,6 +10,22 @@ module Psych
@hash = { :a => 'b' }
end
def test_hash_with_ivars
@hash.instance_variable_set :@foo, 'bar'
dup = Psych.load Psych.dump @hash
assert_equal 'bar', dup.instance_variable_get(:@foo)
end
def test_hash_subclass_with_ivars
x = X.new
x[:a] = 'b'
x.instance_variable_set :@foo, 'bar'
dup = Psych.load Psych.dump x
assert_cycle x
assert_equal 'bar', dup.instance_variable_get(:@foo)
assert_equal X, dup.class
end
def test_load_with_class_syck_compatibility
hash = Psych.load "--- !ruby/object:Hash\n:user_id: 7\n:username: Lucas\n"
assert_equal({ user_id: 7, username: 'Lucas'}, hash)