diff options
author | Gregor Schmidt <ruby@schmidtwisser.de> | 2010-01-26 12:02:31 +0100 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2010-01-27 15:29:06 -0800 |
commit | 7b8c6472ffb6df4d341b0657f82e63c19e9a429c (patch) | |
tree | 4149671cd3573ef37fcf483d87339e08b5febb05 /activesupport/lib | |
parent | b6b3db6734af8d5b42c7bdcea7c73923a5b88463 (diff) | |
download | rails-7b8c6472ffb6df4d341b0657f82e63c19e9a429c.tar.gz rails-7b8c6472ffb6df4d341b0657f82e63c19e9a429c.tar.bz2 rails-7b8c6472ffb6df4d341b0657f82e63c19e9a429c.zip |
Adding custom yaml (de-)serialization for OrderedHash
[#3608 state:committed]
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/ordered_hash.rb | 23 |
1 files changed, 22 insertions, 1 deletions
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 |