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

ruby-changes:22020

From: tenderlove <ko1@a...>
Date: Sun, 18 Dec 2011 12:44:22 +0900 (JST)
Subject: [ruby-changes:22020] tenderlove:r34069 (trunk): * ext/psych/lib/psych/visitors/to_ruby.rb: BigDecimals can be restored

tenderlove	2011-12-18 12:44:09 +0900 (Sun, 18 Dec 2011)

  New Revision: 34069

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

  Log:
    * ext/psych/lib/psych/visitors/to_ruby.rb: BigDecimals can be restored
      from YAML.
    * ext/psych/lib/psych/visitors/yaml_tree.rb: BigDecimals can be dumped
      to YAML.
    * test/psych/test_numeric.rb: tests for BigDecimal serialization

  Modified files:
    trunk/ChangeLog
    trunk/ext/psych/lib/psych/visitors/to_ruby.rb
    trunk/ext/psych/lib/psych/visitors/yaml_tree.rb
    trunk/test/psych/test_numeric.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 34068)
+++ ChangeLog	(revision 34069)
@@ -1,3 +1,11 @@
+Sun Dec 18 12:42:48 2011  Aaron Patterson <aaron@t...>
+
+	* ext/psych/lib/psych/visitors/to_ruby.rb: BigDecimals can be restored
+	  from YAML.
+	* ext/psych/lib/psych/visitors/yaml_tree.rb: BigDecimals can be dumped
+	  to YAML.
+	* test/psych/test_numeric.rb: tests for BigDecimal serialization
+
 Sun Dec 18 12:03:13 2011  Aaron Patterson <aaron@t...>
 
 	* ext/psych/lib/psych/scalar_scanner.rb: Strings that look like dates
Index: ext/psych/lib/psych/visitors/yaml_tree.rb
===================================================================
--- ext/psych/lib/psych/visitors/yaml_tree.rb	(revision 34068)
+++ ext/psych/lib/psych/visitors/yaml_tree.rb	(revision 34069)
@@ -214,6 +214,10 @@
         end
       end
 
+      def visit_BigDecimal o
+        @emitter.scalar o._dump, nil, '!ruby/object:BigDecimal', false, false, Nodes::Scalar::ANY
+      end
+
       def binary? string
         string.encoding == Encoding::ASCII_8BIT ||
           string.index("\x00") ||
Index: ext/psych/lib/psych/visitors/to_ruby.rb
===================================================================
--- ext/psych/lib/psych/visitors/to_ruby.rb	(revision 34068)
+++ ext/psych/lib/psych/visitors/to_ruby.rb	(revision 34069)
@@ -52,6 +52,9 @@
           o.value.unpack('m').first
         when '!str', 'tag:yaml.org,2002:str'
           o.value
+        when '!ruby/object:BigDecimal'
+          require 'bigdecimal'
+          BigDecimal._load o.value
         when "!ruby/object:DateTime"
           require 'date'
           @ss.parse_time(o.value).to_datetime
Index: test/psych/test_numeric.rb
===================================================================
--- test/psych/test_numeric.rb	(revision 34068)
+++ test/psych/test_numeric.rb	(revision 34069)
@@ -1,4 +1,5 @@
 require 'psych/helper'
+require 'bigdecimal'
 
 module Psych
   ###
@@ -10,5 +11,15 @@
       str = Psych.load('--- 090')
       assert_equal '090', str
     end
+
+    def test_big_decimal_tag
+      decimal = BigDecimal("12.34")
+      assert_match "!ruby/object:BigDecimal", Psych.dump(decimal)
+    end
+
+    def test_big_decimal_round_trip
+      decimal = BigDecimal("12.34")
+      assert_cycle decimal
+    end
   end
 end

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

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