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
Brandon Keepers 452cd74d81 Dup keys in OrderedHash to prevent them from being modified [#1676 state:resolved]
Signed-off-by: Frederick Cheung <frederick.cheung@gmail.com>
2009-01-16 17:23:43 +00:00

102 lines
1.7 KiB
Ruby

# 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
class OrderedHash < Hash #:nodoc:
def initialize(*args, &block)
super
@keys = []
end
def initialize_copy(other)
super
# make a deep copy of keys
@keys = other.keys
end
def []=(key, value)
@keys << key if !has_key?(key)
super
end
def delete(key)
if has_key? key
index = @keys.index(key)
@keys.delete_at index
end
super
end
def delete_if
super
sync_keys!
self
end
def reject!
super
sync_keys!
self
end
def reject(&block)
dup.reject!(&block)
end
def keys
@keys.dup
end
def values
@keys.collect { |key| self[key] }
end
def to_hash
Hash.new(self)
end
def each_key
@keys.each { |key| yield key }
end
def each_value
@keys.each { |key| yield self[key]}
end
def each
@keys.each {|key| yield [key, self[key]]}
end
alias_method :each_pair, :each
def clear
super
@keys.clear
self
end
def shift
k = @keys.first
v = delete(k)
[k, v]
end
def merge!(other_hash)
other_hash.each {|k,v| self[k] = v }
self
end
def merge(other_hash)
dup.merge!(other_hash)
end
private
def sync_keys!
@keys.delete_if {|k| !has_key?(k)}
end
end
end
end