diff options
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/ordered_hash.rb | 23 | ||||
-rw-r--r-- | activesupport/test/ordered_hash_test.rb | 24 |
3 files changed, 48 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 710e5c81e0..0454cb731d 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* YAML serialization for OrderedHash. #3608 [Gregor Schmidt] + * Update bundled TZInfo to v0.3.16 [Geoff Buesing] * Georgetown TimeZone is now mapped to "America/Guyana" instead of "America/Argentina/San_Juan" #1821 [Geoff Buesing, Reuben Sivan] diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb index b492648610..6723805d32 100644 --- a/activesupport/lib/active_support/ordered_hash.rb +++ b/activesupport/lib/active_support/ordered_hash.rb @@ -2,7 +2,8 @@ module ActiveSupport # Hash is ordered in Ruby 1.9! if RUBY_VERSION >= '1.9' - OrderedHash = ::Hash + class OrderedHash < ::Hash #:nodoc: + end else class OrderedHash < Hash #:nodoc: def initialize(*args, &block) @@ -138,4 +139,24 @@ module ActiveSupport end end end + + class OrderedHash #:nodoc: + def to_yaml_type + "!tag:yaml.org,2002:omap" + end + + def to_yaml(opts = {}) + YAML.quick_emit(self, opts) do |out| + out.seq(taguri, to_yaml_style) do |seq| + each do |k, v| + seq.add(k => v) + end + end + end + end + end + + YAML.add_builtin_type("omap") do |type, val| + ActiveSupport::OrderedHash[val.map(&:to_a).map(&:first)] + end end diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb index 1521279437..d070206d44 100644 --- a/activesupport/test/ordered_hash_test.rb +++ b/activesupport/test/ordered_hash_test.rb @@ -198,4 +198,28 @@ class OrderedHashTest < Test::Unit::TestCase assert_same original, @ordered_hash assert_equal @other_ordered_hash.keys, @ordered_hash.keys end + + def test_each_after_yaml_serialization + values = [] + @deserialized_ordered_hash = YAML::load(YAML::dump(@ordered_hash)) + + @deserialized_ordered_hash.each {|key, value| values << value} + assert_equal @values, values + end + + def test_order_after_yaml_serialization + @deserialized_ordered_hash = YAML::load(YAML::dump(@ordered_hash)) + + assert_equal @keys, @deserialized_ordered_hash.keys + assert_equal @values, @deserialized_ordered_hash.values + end + + def test_order_after_yaml_serialization_with_nested_arrays + @ordered_hash[:array] = %w(a b c) + + @deserialized_ordered_hash = YAML::load(YAML::dump(@ordered_hash)) + + assert_equal @ordered_hash.keys, @deserialized_ordered_hash.keys + assert_equal @ordered_hash.values, @deserialized_ordered_hash.values + end end |