2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 11:58:50 -04:00
|
|
|
require "yaml"
|
2010-01-27 20:03:05 -05:00
|
|
|
|
2010-04-09 22:33:42 -04:00
|
|
|
YAML.add_builtin_type("omap") do |type, val|
|
2016-08-16 03:30:11 -04:00
|
|
|
ActiveSupport::OrderedHash[val.map { |v| v.to_a.first }]
|
2010-04-09 22:33:42 -04:00
|
|
|
end
|
|
|
|
|
2008-05-14 13:48:37 -04:00
|
|
|
module ActiveSupport
|
2015-12-19 22:43:20 -05:00
|
|
|
# DEPRECATED: <tt>ActiveSupport::OrderedHash</tt> implements a hash that preserves
|
2012-05-16 15:44:57 -04:00
|
|
|
# insertion order.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-08-06 07:11:44 -04:00
|
|
|
# oh = ActiveSupport::OrderedHash.new
|
|
|
|
# oh[:a] = 1
|
|
|
|
# oh[:b] = 2
|
|
|
|
# oh.keys # => [:a, :b], this order is guaranteed
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
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.
|
2021-03-22 18:12:07 -04:00
|
|
|
class OrderedHash < ::Hash # :nodoc:
|
2010-04-09 22:33:42 -04:00
|
|
|
def to_yaml_type
|
|
|
|
"!tag:yaml.org,2002:omap"
|
2010-01-26 06:02:31 -05:00
|
|
|
end
|
2010-04-09 22:33:42 -04:00
|
|
|
|
2011-01-04 17:09:35 -05:00
|
|
|
def encode_with(coder)
|
2016-10-28 23:05:58 -04:00
|
|
|
coder.represent_seq "!omap", map { |k, v| { k => v } }
|
2011-01-04 17:09:35 -05:00
|
|
|
end
|
|
|
|
|
2014-02-26 15:22:52 -05:00
|
|
|
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
|
|
|
|
|
2011-05-08 05:21:06 -04:00
|
|
|
def nested_under_indifferent_access
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2011-09-03 03:43:33 -04:00
|
|
|
# Returns true to make sure that this hash is extractable via <tt>Array#extract_options!</tt>
|
|
|
|
def extractable_options?
|
|
|
|
true
|
|
|
|
end
|
2010-01-26 06:02:31 -05:00
|
|
|
end
|
2008-05-14 13:48:37 -04:00
|
|
|
end
|