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: Added ability to load array
subclasses with ivars. * ext/psych/lib/psych/visitors/yaml_tree.rb: Added ability to dump array subclasses with ivars. * test/psych/test_array.rb: corresponding tests git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34328 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e58d77bdda
commit
ee3ebc2de5
4 changed files with 92 additions and 3 deletions
|
@ -1,3 +1,11 @@
|
||||||
|
Wed Jan 18 10:39:47 2012 Aaron Patterson <aaron@tenderlovemaking.com>
|
||||||
|
|
||||||
|
* ext/psych/lib/psych/visitors/to_ruby.rb: Added ability to load array
|
||||||
|
subclasses with ivars.
|
||||||
|
* ext/psych/lib/psych/visitors/yaml_tree.rb: Added ability to dump
|
||||||
|
array subclasses with ivars.
|
||||||
|
* test/psych/test_array.rb: corresponding tests
|
||||||
|
|
||||||
Tue Jan 17 17:18:41 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Tue Jan 17 17:18:41 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* configure.in (SPT_TYPE): enable as SPT_REUSEARGV on Darwin.
|
* configure.in (SPT_TYPE): enable as SPT_REUSEARGV on Darwin.
|
||||||
|
|
|
@ -119,6 +119,11 @@ module Psych
|
||||||
map[accept(a.children.first)] = accept a.children.last
|
map[accept(a.children.first)] = accept a.children.last
|
||||||
}
|
}
|
||||||
map
|
map
|
||||||
|
when /^!(?:seq|ruby\/array):(.*)$/
|
||||||
|
klass = resolve_class($1)
|
||||||
|
list = register(o, klass.allocate)
|
||||||
|
o.children.each { |c| list.push accept c }
|
||||||
|
list
|
||||||
else
|
else
|
||||||
list = register(o, [])
|
list = register(o, [])
|
||||||
o.children.each { |c| list.push accept c }
|
o.children.each { |c| list.push accept c }
|
||||||
|
@ -135,6 +140,17 @@ module Psych
|
||||||
members = Hash[*o.children.map { |c| accept c }]
|
members = Hash[*o.children.map { |c| accept c }]
|
||||||
string = members.delete 'str'
|
string = members.delete 'str'
|
||||||
init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] }, o)
|
init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] }, o)
|
||||||
|
when /^!ruby\/array:(.*)$/
|
||||||
|
klass = resolve_class($1)
|
||||||
|
list = register(o, klass.allocate)
|
||||||
|
|
||||||
|
members = Hash[o.children.map { |c| accept c }.each_slice(2).to_a]
|
||||||
|
list.replace members['internal']
|
||||||
|
|
||||||
|
members['ivars'].each do |ivar, v|
|
||||||
|
list.instance_variable_set ivar, v
|
||||||
|
end
|
||||||
|
list
|
||||||
when /^!ruby\/struct:?(.*)?$/
|
when /^!ruby\/struct:?(.*)?$/
|
||||||
klass = resolve_class($1)
|
klass = resolve_class($1)
|
||||||
|
|
||||||
|
|
|
@ -301,9 +301,13 @@ module Psych
|
||||||
end
|
end
|
||||||
|
|
||||||
def visit_Array o
|
def visit_Array o
|
||||||
register o, @emitter.start_sequence(nil, nil, true, Nodes::Sequence::BLOCK)
|
if o.class == ::Array
|
||||||
o.each { |c| accept c }
|
register o, @emitter.start_sequence(nil, nil, true, Nodes::Sequence::BLOCK)
|
||||||
@emitter.end_sequence
|
o.each { |c| accept c }
|
||||||
|
@emitter.end_sequence
|
||||||
|
else
|
||||||
|
visit_array_subclass o
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def visit_NilClass o
|
def visit_NilClass o
|
||||||
|
@ -315,6 +319,39 @@ module Psych
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
def visit_array_subclass o
|
||||||
|
tag = "!ruby/array:#{o.class}"
|
||||||
|
if o.instance_variables.empty?
|
||||||
|
node = @emitter.start_sequence(nil, tag, false, Nodes::Sequence::BLOCK)
|
||||||
|
register o, node
|
||||||
|
o.each { |c| accept c }
|
||||||
|
@emitter.end_sequence
|
||||||
|
else
|
||||||
|
node = @emitter.start_mapping(nil, tag, false, Nodes::Sequence::BLOCK)
|
||||||
|
register o, node
|
||||||
|
|
||||||
|
# Dump the internal list
|
||||||
|
accept 'internal'
|
||||||
|
@emitter.start_sequence(nil, nil, true, Nodes::Sequence::BLOCK)
|
||||||
|
o.each { |c| accept c }
|
||||||
|
@emitter.end_sequence
|
||||||
|
|
||||||
|
# Dump the ivars
|
||||||
|
accept 'ivars'
|
||||||
|
@emitter.start_mapping(nil, nil, true, Nodes::Sequence::BLOCK)
|
||||||
|
o.instance_variables.each do |ivar|
|
||||||
|
accept ivar
|
||||||
|
accept o.instance_variable_get ivar
|
||||||
|
end
|
||||||
|
@emitter.end_mapping
|
||||||
|
|
||||||
|
@emitter.end_mapping
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def dump_list o
|
||||||
|
end
|
||||||
|
|
||||||
# '%:z' was no defined until 1.9.3
|
# '%:z' was no defined until 1.9.3
|
||||||
if RUBY_VERSION < '1.9.3'
|
if RUBY_VERSION < '1.9.3'
|
||||||
def format_time time
|
def format_time time
|
||||||
|
|
|
@ -2,11 +2,39 @@ require 'psych/helper'
|
||||||
|
|
||||||
module Psych
|
module Psych
|
||||||
class TestArray < TestCase
|
class TestArray < TestCase
|
||||||
|
class X < Array
|
||||||
|
end
|
||||||
|
|
||||||
|
class Y < Array
|
||||||
|
attr_accessor :val
|
||||||
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
super
|
super
|
||||||
@list = [{ :a => 'b' }, 'foo']
|
@list = [{ :a => 'b' }, 'foo']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_subclass
|
||||||
|
yaml = Psych.dump X.new
|
||||||
|
assert_match X.name, yaml
|
||||||
|
|
||||||
|
list = X.new
|
||||||
|
list << 1
|
||||||
|
assert_equal X, list.class
|
||||||
|
assert_equal 1, list.first
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_subclass_with_attributes
|
||||||
|
y = Psych.load Psych.dump Y.new.tap {|y| y.val = 1}
|
||||||
|
assert_equal Y, y.class
|
||||||
|
assert_equal 1, y.val
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_backwards_with_syck
|
||||||
|
x = Psych.load "--- !seq:#{X.name} []\n\n"
|
||||||
|
assert_equal X, x.class
|
||||||
|
end
|
||||||
|
|
||||||
def test_self_referential
|
def test_self_referential
|
||||||
@list << @list
|
@list << @list
|
||||||
assert_cycle(@list)
|
assert_cycle(@list)
|
||||||
|
|
Loading…
Reference in a new issue