2008-05-14 13:48:37 -04:00
|
|
|
# OrderedHash is namespaced to prevent conflicts with other implementations
|
|
|
|
module ActiveSupport
|
|
|
|
# Hash is ordered in Ruby 1.9!
|
|
|
|
if RUBY_VERSION >= '1.9'
|
|
|
|
OrderedHash = ::Hash
|
|
|
|
else
|
2008-12-09 15:36:24 -05:00
|
|
|
class OrderedHash < Hash #:nodoc:
|
|
|
|
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
|
|
|
|
args.each_with_index { |val,ind|
|
|
|
|
# Only every second value is a key.
|
|
|
|
next if ind % 2 != 0
|
|
|
|
ordered_hash[val] = args[ind + 1]
|
|
|
|
}
|
|
|
|
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)
|
2009-01-04 09:01:23 -05:00
|
|
|
@keys << key if !has_key?(key)
|
2008-12-09 15:36:24 -05:00
|
|
|
super
|
2008-05-14 13:48:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete(key)
|
2008-12-11 10:19:03 -05:00
|
|
|
if has_key? key
|
|
|
|
index = @keys.index(key)
|
|
|
|
@keys.delete_at index
|
2008-12-09 15:36:24 -05:00
|
|
|
end
|
|
|
|
super
|
2008-05-14 13:48:37 -04:00
|
|
|
end
|
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
|
2008-12-09 15:36:24 -05:00
|
|
|
@keys.each { |key| yield key }
|
2008-11-18 17:00:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def each_value
|
2008-12-09 15:36:24 -05:00
|
|
|
@keys.each { |key| yield self[key]}
|
|
|
|
end
|
|
|
|
|
|
|
|
def each
|
2009-01-04 09:01:23 -05:00
|
|
|
@keys.each {|key| yield [key, self[key]]}
|
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
|
|
|
|
|
|
|
|
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)
|
|
|
|
other_hash.each {|k,v| self[k] = v }
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2008-12-11 10:19:03 -05:00
|
|
|
def merge(other_hash)
|
2009-01-04 09:01:23 -05:00
|
|
|
dup.merge!(other_hash)
|
2008-12-11 10:19:03 -05:00
|
|
|
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
|
|
|
|
|
2009-01-04 09:01:23 -05:00
|
|
|
private
|
2008-12-11 08:12:06 -05:00
|
|
|
|
|
|
|
def sync_keys!
|
2008-12-11 10:19:03 -05:00
|
|
|
@keys.delete_if {|k| !has_key?(k)}
|
2008-12-11 08:12:06 -05:00
|
|
|
end
|
2008-05-14 13:48:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|