[前][次][番号順一覧][スレッド一覧]

ruby-changes:18585

From: tenderlove <ko1@a...>
Date: Thu, 20 Jan 2011 09:10:25 +0900 (JST)
Subject: [ruby-changes:18585] Ruby:r30609 (trunk): * ext/psych/lib/psych/coder.rb (represent_object): arbitrary objects

tenderlove	2011-01-20 08:05:53 +0900 (Thu, 20 Jan 2011)

  New Revision: 30609

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=30609

  Log:
    * 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.

  Modified files:
    trunk/ChangeLog
    trunk/ext/psych/lib/psych/coder.rb
    trunk/ext/psych/lib/psych/visitors/yaml_tree.rb
    trunk/test/psych/test_coder.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 30608)
+++ ChangeLog	(revision 30609)
@@ -1,3 +1,13 @@
+Thu Jan 20 08:02:46 2011  Aaron Patterson <aaron@t...>
+
+	* 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@f...>
 
 	* method.h: parenthesize macro arguments.
Index: ext/psych/lib/psych/visitors/yaml_tree.rb
===================================================================
--- ext/psych/lib/psych/visitors/yaml_tree.rb	(revision 30608)
+++ ext/psych/lib/psych/visitors/yaml_tree.rb	(revision 30609)
@@ -342,6 +342,8 @@
             accept v
           end
           @emitter.end_mapping
+        when :object
+          accept c.object
         end
       end
 
Index: ext/psych/lib/psych/coder.rb
===================================================================
--- ext/psych/lib/psych/coder.rb	(revision 30608)
+++ ext/psych/lib/psych/coder.rb	(revision 30609)
@@ -6,17 +6,18 @@
   # objects like Sequence and Scalar may be emitted if +seq=+ or +scalar=+ are
   # called, respectively.
   class Coder
-    attr_accessor :tag, :style, :implicit
+    attr_accessor :tag, :style, :implicit, :object
     attr_reader   :type, :seq
 
     def initialize tag
-      @map        = {}
-      @seq        = []
-      @implicit   = false
-      @type       = :map
-      @tag        = tag
-      @style      = Psych::Nodes::Mapping::BLOCK
-      @scalar     = nil
+      @map      = {}
+      @seq      = []
+      @implicit = false
+      @type     = :map
+      @tag      = tag
+      @style    = Psych::Nodes::Mapping::BLOCK
+      @scalar   = nil
+      @object   = nil
     end
 
     def scalar *args
@@ -54,6 +55,13 @@
       self.map = map
     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+
     def scalar= value
       @type   = :scalar
Index: test/psych/test_coder.rb
===================================================================
--- test/psych/test_coder.rb	(revision 30608)
+++ test/psych/test_coder.rb	(revision 30609)
@@ -89,6 +89,17 @@
       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
       refute_match('TestCoder::InitApi', Psych.to_json(InitApi.new))
     end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]