mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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:
parent
2b2f9b75fe
commit
8c08c8298a
4 changed files with 76 additions and 8 deletions
|
@ -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>
|
Thu Jan 8 17:05:00 2015 Seiei Higa <hanachin@gmail.com>
|
||||||
|
|
||||||
* vm_method.c (rb_method_entry): if no super class, no original
|
* vm_method.c (rb_method_entry): if no super class, no original
|
||||||
|
|
|
@ -261,6 +261,20 @@ module Psych
|
||||||
end
|
end
|
||||||
set
|
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:(.*)$/
|
when /^!map:(.*)$/, /^!ruby\/hash:(.*)$/
|
||||||
revive_hash register(o, resolve_class($1).new), o
|
revive_hash register(o, resolve_class($1).new), o
|
||||||
|
|
||||||
|
|
|
@ -367,17 +367,46 @@ module Psych
|
||||||
end
|
end
|
||||||
|
|
||||||
def visit_Hash o
|
def visit_Hash o
|
||||||
tag = o.class == ::Hash ? nil : "!ruby/hash:#{o.class}"
|
ivars = o.instance_variables
|
||||||
implicit = !tag
|
|
||||||
|
|
||||||
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|
|
register(o, @emitter.start_mapping(nil, tag, false, Psych::Nodes::Mapping::BLOCK))
|
||||||
accept k
|
|
||||||
accept v
|
@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
|
end
|
||||||
|
|
||||||
@emitter.end_mapping
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def visit_Psych_Set o
|
def visit_Psych_Set o
|
||||||
|
|
|
@ -10,6 +10,22 @@ module Psych
|
||||||
@hash = { :a => 'b' }
|
@hash = { :a => 'b' }
|
||||||
end
|
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
|
def test_load_with_class_syck_compatibility
|
||||||
hash = Psych.load "--- !ruby/object:Hash\n:user_id: 7\n:username: Lucas\n"
|
hash = Psych.load "--- !ruby/object:Hash\n:user_id: 7\n:username: Lucas\n"
|
||||||
assert_equal({ user_id: 7, username: 'Lucas'}, hash)
|
assert_equal({ user_id: 7, username: 'Lucas'}, hash)
|
||||||
|
|
Loading…
Add table
Reference in a new issue