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

ruby-changes:18606

From: tenderlove <ko1@a...>
Date: Sat, 22 Jan 2011 12:00:47 +0900 (JST)
Subject: [ruby-changes:18606] Ruby:r30630 (trunk): * ext/psych/lib/psych/visitors/to_ruby.rb: fixing merge key support

tenderlove	2011-01-22 11:51:14 +0900 (Sat, 22 Jan 2011)

  New Revision: 30630

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

  Log:
    * ext/psych/lib/psych/visitors/to_ruby.rb: fixing merge key support
      when multiple merge keys are specified.
    * test/psych/test_merge_keys.rb: tests for multi-merge key support

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 30629)
+++ ChangeLog	(revision 30630)
@@ -1,3 +1,10 @@
+Sat Jan 22 11:49:55 2011  Aaron Patterson <aaron@t...>
+
+	* ext/psych/lib/psych/visitors/to_ruby.rb: fixing merge key support
+	  when multiple merge keys are specified.
+
+	* test/psych/test_merge_keys.rb: tests for multi-merge key support
+	
 Sat Jan 22 11:33:04 2011  Aaron Patterson <aaron@t...>
 
 	* ext/psych/lib/psych/visitors/to_ruby.rb: merge keys are actually
Index: ext/psych/lib/psych/visitors/to_ruby.rb
===================================================================
--- ext/psych/lib/psych/visitors/to_ruby.rb	(revision 30629)
+++ ext/psych/lib/psych/visitors/to_ruby.rb	(revision 30630)
@@ -187,8 +187,17 @@
           o.children.each_slice(2) { |k,v|
             key = accept(k)
 
-            if key == '<<' && Nodes::Alias === v
-              hash.merge! accept(v)
+            if key == '<<'
+              case v
+              when Nodes::Alias
+                hash.merge! accept(v)
+              when Nodes::Sequence
+                accept(v).reverse_each do |value|
+                  hash.merge! value
+                end
+              else
+                hash[key] = accept(v)
+              end
             else
               hash[key] = accept(v)
             end
Index: test/psych/test_merge_keys.rb
===================================================================
--- test/psych/test_merge_keys.rb	(revision 30629)
+++ test/psych/test_merge_keys.rb	(revision 30630)
@@ -17,5 +17,56 @@
         "bar" => { "hello" => "world", "baz" => "boo" } }
       assert_equal hash, Psych.load(yaml)
     end
+
+    def test_multiple_maps
+      yaml = <<-eoyaml
+---
+- &CENTER { x: 1, y: 2 }
+- &LEFT { x: 0, y: 2 }
+- &BIG { r: 10 }
+- &SMALL { r: 1 }
+
+# All the following maps are equal:
+
+- # Merge multiple maps
+  << : [ *CENTER, *BIG ]
+  label: center/big
+      eoyaml
+
+      hash = {
+        'x' => 1,
+        'y' => 2,
+        'r' => 10,
+        'label' => 'center/big'
+      }
+
+      assert_equal hash, Psych.load(yaml)[4]
+    end
+
+    def test_override
+      yaml = <<-eoyaml
+---
+- &CENTER { x: 1, y: 2 }
+- &LEFT { x: 0, y: 2 }
+- &BIG { r: 10 }
+- &SMALL { r: 1 }
+
+# All the following maps are equal:
+
+- # Override
+  << : [ *BIG, *LEFT, *SMALL ]
+  x: 1
+  label: center/big
+      eoyaml
+
+      hash = {
+        'x' => 1,
+        'y' => 2,
+        'r' => 10,
+        'label' => 'center/big'
+      }
+
+      assert_equal hash, Psych.load(yaml)[4]
+    end
   end
 end

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

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