Sha256: c0f6c34cae6fbd9d8873903e4f22edc6bd8d67847728b4b5ebaaf9ef743da6f0

Contents?: true

Size: 1.01 KB

Versions: 14

Compression:

Stored size: 1.01 KB

Contents

require 'yaml'

YAML.add_builtin_type("omap") do |type, val|
  ActiveSupport::OrderedHash[val.map{ |v| v.to_a.first }]
end

module ActiveSupport
  # <tt>ActiveSupport::OrderedHash</tt> implements a hash that preserves
  # insertion order.
  #
  #   oh = ActiveSupport::OrderedHash.new
  #   oh[:a] = 1
  #   oh[:b] = 2
  #   oh.keys # => [:a, :b], this order is guaranteed
  #
  # Also, maps the +omap+ feature for YAML files
  # (See http://yaml.org/type/omap.html) to support ordered items
  # when loading from yaml.
  #
  # <tt>ActiveSupport::OrderedHash</tt> is namespaced to prevent conflicts
  # with other implementations.
  class OrderedHash < ::Hash
    def to_yaml_type
      "!tag:yaml.org,2002:omap"
    end

    def encode_with(coder)
      coder.represent_seq '!omap', map { |k,v| { k => v } }
    end

    def nested_under_indifferent_access
      self
    end

    # Returns true to make sure that this hash is extractable via <tt>Array#extract_options!</tt>
    def extractable_options?
      true
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
activesupport-4.0.3 lib/active_support/ordered_hash.rb
activesupport-4.1.0.beta2 lib/active_support/ordered_hash.rb
activesupport-4.1.0.beta1 lib/active_support/ordered_hash.rb
activesupport-4.0.2 lib/active_support/ordered_hash.rb
activesupport-4.0.1 lib/active_support/ordered_hash.rb
activesupport-4.0.1.rc4 lib/active_support/ordered_hash.rb
activesupport-4.0.1.rc3 lib/active_support/ordered_hash.rb
activesupport-4.0.1.rc2 lib/active_support/ordered_hash.rb
activesupport-4.0.1.rc1 lib/active_support/ordered_hash.rb
challah-1.0.0 vendor/bundle/gems/activesupport-4.0.0/lib/active_support/ordered_hash.rb
activesupport-4.0.0 lib/active_support/ordered_hash.rb
activesupport-4.0.0.rc2 lib/active_support/ordered_hash.rb
activesupport-4.0.0.rc1 lib/active_support/ordered_hash.rb
activesupport-4.0.0.beta1 lib/active_support/ordered_hash.rb