2011-01-28 17:24:11 -05:00
|
|
|
begin
|
|
|
|
require 'psych'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
2010-01-27 20:03:05 -05:00
|
|
|
require 'yaml'
|
|
|
|
|
2010-04-09 22:33:42 -04:00
|
|
|
YAML.add_builtin_type("omap") do |type, val|
|
|
|
|
ActiveSupport::OrderedHash[val.map(&:to_a).map(&:first)]
|
|
|
|
end
|
|
|
|
|
2008-05-14 13:48:37 -04:00
|
|
|
module ActiveSupport
|
2010-08-06 07:11:44 -04:00
|
|
|
# The order of iteration over hashes in Ruby 1.8 is undefined. For example, you do not know the
|
|
|
|
# order in which +keys+ will return keys, or +each+ yield pairs. <tt>ActiveSupport::OrderedHash</tt>
|
|
|
|
# implements a hash that preserves insertion order, as in Ruby 1.9:
|
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
|
|
|
#
|
2010-08-06 07:11:44 -04:00
|
|
|
# <tt>ActiveSupport::OrderedHash</tt> is namespaced to prevent conflicts with other implementations.
|
2010-04-09 22:33:42 -04:00
|
|
|
class OrderedHash < ::Hash #:nodoc:
|
|
|
|
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)
|
|
|
|
coder.represent_seq '!omap', map { |k,v| { k => v } }
|
|
|
|
end
|
|
|
|
|
2010-04-09 22:33:42 -04:00
|
|
|
def to_yaml(opts = {})
|
2011-01-04 17:09:35 -05:00
|
|
|
if YAML.const_defined?(:ENGINE) && !YAML::ENGINE.syck?
|
|
|
|
return super
|
|
|
|
end
|
|
|
|
|
2010-04-09 22:33:42 -04:00
|
|
|
YAML.quick_emit(self, opts) do |out|
|
2011-01-04 17:09:35 -05:00
|
|
|
out.seq(taguri) do |seq|
|
2010-04-09 22:33:42 -04:00
|
|
|
each do |k, v|
|
|
|
|
seq.add(k => v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Hash is ordered in Ruby 1.9!
|
|
|
|
if RUBY_VERSION < '1.9'
|
2010-06-13 16:02:29 -04:00
|
|
|
|
|
|
|
# In MRI the Hash class is core and written in C. In particular, methods are
|
|
|
|
# programmed with explicit C function calls and polymorphism is not honored.
|
|
|
|
#
|
|
|
|
# For example, []= is crucial in this implementation to maintain the @keys
|
|
|
|
# array but hash.c invokes rb_hash_aset() originally. This prevents method
|
|
|
|
# reuse through inheritance and forces us to reimplement stuff.
|
|
|
|
#
|
|
|
|
# For instance, we cannot use the inherited #merge! because albeit the algorithm
|
|
|
|
# itself would work, our []= is not being called at all by the C code.
|
|
|
|
|
2008-12-09 15:36:24 -05:00
|
|
|
def initialize(*args, &block)
|
|
|
|
super
|
|
|
|
@keys = []
|
2008-05-14 13:48:37 -04:00
|
|
|
end
|
|
|
|
|
2009-05-07 18:58:07 -04:00
|
|
|
def self.[](*args)
|
|
|
|
ordered_hash = new
|
2009-06-24 13:51:20 -04:00
|
|
|
|
|
|
|
if (args.length == 1 && args.first.is_a?(Array))
|
|
|
|
args.first.each do |key_value_pair|
|
|
|
|
next unless (key_value_pair.is_a?(Array))
|
|
|
|
ordered_hash[key_value_pair[0]] = key_value_pair[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
return ordered_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
unless (args.size % 2 == 0)
|
|
|
|
raise ArgumentError.new("odd number of arguments for Hash")
|
|
|
|
end
|
|
|
|
|
2010-10-14 00:06:07 -04:00
|
|
|
args.each_with_index do |val, ind|
|
|
|
|
next if (ind % 2 != 0)
|
|
|
|
ordered_hash[val] = args[ind + 1]
|
2009-06-24 13:51:20 -04:00
|
|
|
end
|
|
|
|
|
2009-05-07 18:58:07 -04:00
|
|
|
ordered_hash
|
|
|
|
end
|
|
|
|
|
2009-01-04 09:01:23 -05:00
|
|
|
def initialize_copy(other)
|
|
|
|
super
|
|
|
|
# make a deep copy of keys
|
|
|
|
@keys = other.keys
|
|
|
|
end
|
|
|
|
|
2008-12-09 15:36:24 -05:00
|
|
|
def []=(key, value)
|
2010-10-13 23:44:26 -04:00
|
|
|
@keys << key unless has_key?(key)
|
2008-12-09 15:36:24 -05:00
|
|
|
super
|
2008-05-14 13:48:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete(key)
|
2010-10-15 13:23:05 -04:00
|
|
|
if has_key? key
|
|
|
|
index = @keys.index(key)
|
|
|
|
@keys.delete_at index
|
|
|
|
end
|
2008-12-09 15:36:24 -05:00
|
|
|
super
|
2008-05-14 13:48:37 -04:00
|
|
|
end
|
2010-04-09 22:33:42 -04:00
|
|
|
|
2009-01-04 09:01:23 -05:00
|
|
|
def delete_if
|
|
|
|
super
|
|
|
|
sync_keys!
|
|
|
|
self
|
|
|
|
end
|
2008-05-14 13:48:37 -04:00
|
|
|
|
2008-12-11 08:12:06 -05:00
|
|
|
def reject!
|
|
|
|
super
|
|
|
|
sync_keys!
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def reject(&block)
|
|
|
|
dup.reject!(&block)
|
|
|
|
end
|
|
|
|
|
2008-05-14 13:48:37 -04:00
|
|
|
def keys
|
2009-01-04 09:01:23 -05:00
|
|
|
@keys.dup
|
2008-05-14 13:48:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def values
|
2008-12-09 15:36:24 -05:00
|
|
|
@keys.collect { |key| self[key] }
|
2008-05-14 13:48:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_hash
|
2009-03-03 22:10:33 -05:00
|
|
|
self
|
2008-06-03 15:15:33 -04:00
|
|
|
end
|
|
|
|
|
2009-05-09 18:35:31 -04:00
|
|
|
def to_a
|
|
|
|
@keys.map { |key| [ key, self[key] ] }
|
|
|
|
end
|
|
|
|
|
2008-11-18 17:00:35 -05:00
|
|
|
def each_key
|
2011-02-02 23:35:54 -05:00
|
|
|
return to_enum(:each_key) unless block_given?
|
2008-12-09 15:36:24 -05:00
|
|
|
@keys.each { |key| yield key }
|
2011-02-02 09:58:40 -05:00
|
|
|
self
|
2008-11-18 17:00:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def each_value
|
2011-02-02 23:35:54 -05:00
|
|
|
return to_enum(:each_value) unless block_given?
|
2008-12-09 15:36:24 -05:00
|
|
|
@keys.each { |key| yield self[key]}
|
2011-02-02 09:58:40 -05:00
|
|
|
self
|
2008-12-09 15:36:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def each
|
2011-02-02 23:35:54 -05:00
|
|
|
return to_enum(:each) unless block_given?
|
2009-01-04 09:01:23 -05:00
|
|
|
@keys.each {|key| yield [key, self[key]]}
|
2011-02-02 09:58:40 -05:00
|
|
|
self
|
2008-11-18 17:00:35 -05:00
|
|
|
end
|
2008-12-11 08:12:06 -05:00
|
|
|
|
2008-12-11 10:19:03 -05:00
|
|
|
alias_method :each_pair, :each
|
|
|
|
|
2010-10-20 11:01:02 -04:00
|
|
|
alias_method :select, :find_all
|
|
|
|
|
2008-12-11 10:19:03 -05:00
|
|
|
def clear
|
|
|
|
super
|
|
|
|
@keys.clear
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def shift
|
|
|
|
k = @keys.first
|
|
|
|
v = delete(k)
|
|
|
|
[k, v]
|
|
|
|
end
|
|
|
|
|
2009-01-04 09:01:23 -05:00
|
|
|
def merge!(other_hash)
|
2010-06-13 01:12:12 -04:00
|
|
|
if block_given?
|
|
|
|
other_hash.each { |k, v| self[k] = key?(k) ? yield(k, self[k], v) : v }
|
|
|
|
else
|
|
|
|
other_hash.each { |k, v| self[k] = v }
|
2010-06-11 18:38:18 -04:00
|
|
|
end
|
2009-01-04 09:01:23 -05:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2010-06-25 10:35:11 -04:00
|
|
|
alias_method :update, :merge!
|
|
|
|
|
2010-06-11 18:38:18 -04:00
|
|
|
def merge(other_hash, &block)
|
|
|
|
dup.merge!(other_hash, &block)
|
2008-12-11 10:19:03 -05:00
|
|
|
end
|
|
|
|
|
2009-11-04 10:13:59 -05:00
|
|
|
# When replacing with another hash, the initial order of our keys must come from the other hash -ordered or not.
|
|
|
|
def replace(other)
|
|
|
|
super
|
|
|
|
@keys = other.keys
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2010-06-26 04:52:20 -04:00
|
|
|
def invert
|
|
|
|
OrderedHash[self.to_a.map!{|key_value_pair| key_value_pair.reverse}]
|
|
|
|
end
|
|
|
|
|
2009-01-19 14:44:55 -05:00
|
|
|
def inspect
|
2009-03-04 19:20:49 -05:00
|
|
|
"#<OrderedHash #{super}>"
|
2009-01-19 14:44:55 -05:00
|
|
|
end
|
|
|
|
|
2010-04-09 22:33:42 -04:00
|
|
|
private
|
|
|
|
def sync_keys!
|
|
|
|
@keys.delete_if {|k| !has_key?(k)}
|
2010-01-26 06:02:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-05-14 13:48:37 -04:00
|
|
|
end
|