mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* ext/psych/lib/psych/coder.rb (represent_object): arbitrary objects
may be passed to the Psych::Coder object. * ext/psych/lib/psych/visitors/yaml_tree.rb: support for visiting arbitrary objects set on the coder. * test/psych/test_coder.rb: supporting test case. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30609 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
64847a9cfe
commit
a17cdfdde2
4 changed files with 39 additions and 8 deletions
10
ChangeLog
10
ChangeLog
|
@ -1,3 +1,13 @@
|
||||||
|
Thu Jan 20 08:02:46 2011 Aaron Patterson <aaron@tenderlovemaking.com>
|
||||||
|
|
||||||
|
* ext/psych/lib/psych/coder.rb (represent_object): arbitrary objects
|
||||||
|
may be passed to the Psych::Coder object.
|
||||||
|
|
||||||
|
* ext/psych/lib/psych/visitors/yaml_tree.rb: support for visiting
|
||||||
|
arbitrary objects set on the coder.
|
||||||
|
|
||||||
|
* test/psych/test_coder.rb: supporting test case.
|
||||||
|
|
||||||
Thu Jan 20 06:03:17 2011 Tanaka Akira <akr@fsij.org>
|
Thu Jan 20 06:03:17 2011 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
* method.h: parenthesize macro arguments.
|
* method.h: parenthesize macro arguments.
|
||||||
|
|
|
@ -6,17 +6,18 @@ module Psych
|
||||||
# objects like Sequence and Scalar may be emitted if +seq=+ or +scalar=+ are
|
# objects like Sequence and Scalar may be emitted if +seq=+ or +scalar=+ are
|
||||||
# called, respectively.
|
# called, respectively.
|
||||||
class Coder
|
class Coder
|
||||||
attr_accessor :tag, :style, :implicit
|
attr_accessor :tag, :style, :implicit, :object
|
||||||
attr_reader :type, :seq
|
attr_reader :type, :seq
|
||||||
|
|
||||||
def initialize tag
|
def initialize tag
|
||||||
@map = {}
|
@map = {}
|
||||||
@seq = []
|
@seq = []
|
||||||
@implicit = false
|
@implicit = false
|
||||||
@type = :map
|
@type = :map
|
||||||
@tag = tag
|
@tag = tag
|
||||||
@style = Psych::Nodes::Mapping::BLOCK
|
@style = Psych::Nodes::Mapping::BLOCK
|
||||||
@scalar = nil
|
@scalar = nil
|
||||||
|
@object = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def scalar *args
|
def scalar *args
|
||||||
|
@ -54,6 +55,13 @@ module Psych
|
||||||
self.map = map
|
self.map = map
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Emit an arbitrary object +obj+ and +tag+
|
||||||
|
def represent_object tag, obj
|
||||||
|
@tag = tag
|
||||||
|
@type = :object
|
||||||
|
@object = obj
|
||||||
|
end
|
||||||
|
|
||||||
# Emit a scalar with +value+
|
# Emit a scalar with +value+
|
||||||
def scalar= value
|
def scalar= value
|
||||||
@type = :scalar
|
@type = :scalar
|
||||||
|
|
|
@ -342,6 +342,8 @@ module Psych
|
||||||
accept v
|
accept v
|
||||||
end
|
end
|
||||||
@emitter.end_mapping
|
@emitter.end_mapping
|
||||||
|
when :object
|
||||||
|
accept c.object
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -89,6 +89,17 @@ module Psych
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class RepresentWithObject
|
||||||
|
def encode_with coder
|
||||||
|
coder.represent_object self.class.name, 20
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_represent_with_object
|
||||||
|
thing = Psych.load(Psych.dump(RepresentWithObject.new))
|
||||||
|
assert_equal 20, thing
|
||||||
|
end
|
||||||
|
|
||||||
def test_json_dump_exclude_tag
|
def test_json_dump_exclude_tag
|
||||||
refute_match('TestCoder::InitApi', Psych.to_json(InitApi.new))
|
refute_match('TestCoder::InitApi', Psych.to_json(InitApi.new))
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue