Sha256: 34439ef3868b582d32e38670f102839af45d32eb02c02e0ba16602ce5fc579fb

Contents?: true

Size: 1.19 KB

Versions: 119

Compression:

Stored size: 1.19 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 select(*args, &block)
      dup.tap { |hash| hash.select!(*args, &block) }
    end

    def reject(*args, &block)
      dup.tap { |hash| hash.reject!(*args, &block) }
    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

119 entries across 114 versions & 12 rubygems

Version Path
activesupport-4.1.15 lib/active_support/ordered_hash.rb
activesupport-4.2.6 lib/active_support/ordered_hash.rb
activesupport-4.1.15.rc1 lib/active_support/ordered_hash.rb
activesupport-4.2.6.rc1 lib/active_support/ordered_hash.rb
activesupport-4.1.14.2 lib/active_support/ordered_hash.rb
activesupport-4.2.5.2 lib/active_support/ordered_hash.rb
activejob-lock-0.0.2 rails/activesupport/lib/active_support/ordered_hash.rb
activesupport-5.0.0.beta1.1 lib/active_support/ordered_hash.rb
activesupport-4.2.5.1 lib/active_support/ordered_hash.rb
activesupport-4.1.14.1 lib/active_support/ordered_hash.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/activesupport-4.2.4/lib/active_support/ordered_hash.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/activesupport-4.1.13/lib/active_support/ordered_hash.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/activesupport-4.2.4/lib/active_support/ordered_hash.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/activesupport-4.1.13/lib/active_support/ordered_hash.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/ordered_hash.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5/lib/active_support/ordered_hash.rb
activesupport-5.0.0.beta1 lib/active_support/ordered_hash.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/activesupport-4.2.4/lib/active_support/ordered_hash.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/activesupport-4.1.13/lib/active_support/ordered_hash.rb
activesupport-4.1.14 lib/active_support/ordered_hash.rb