1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activesupport/lib/active_support/ordered_hash.rb

51 lines
1.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "yaml"
2010-04-09 22:33:42 -04:00
YAML.add_builtin_type("omap") do |type, val|
ActiveSupport::OrderedHash[val.map { |v| v.to_a.first }]
2010-04-09 22:33:42 -04:00
end
module ActiveSupport
# DEPRECATED: <tt>ActiveSupport::OrderedHash</tt> implements a hash that preserves
2012-05-16 15:44:57 -04:00
# insertion order.
#
# oh = ActiveSupport::OrderedHash.new
# oh[:a] = 1
# oh[:b] = 2
# oh.keys # => [:a, :b], this order is guaranteed
#
2012-05-16 15:44:57 -04:00
# Also, maps the +omap+ feature for YAML files
2019-10-02 05:54:57 -04:00
# (See https://yaml.org/type/omap.html) to support ordered items
2012-05-16 15:44:57 -04:00
# when loading from yaml.
#
# <tt>ActiveSupport::OrderedHash</tt> is namespaced to prevent conflicts
# with other implementations.
class OrderedHash < ::Hash # :nodoc:
2010-04-09 22:33:42 -04:00
def to_yaml_type
"!tag:yaml.org,2002:omap"
end
2010-04-09 22:33:42 -04:00
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
2014-01-16 21:10:25 -05:00
def reject(*args, &block)
2014-01-16 21:30:41 -05:00
dup.tap { |hash| hash.reject!(*args, &block) }
2014-01-16 21:10:25 -05:00
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