aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/ordered_hash.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/ordered_hash.rb')
-rw-r--r--activesupport/lib/active_support/ordered_hash.rb85
1 files changed, 54 insertions, 31 deletions
diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb
index 5de94c67e0..3def0be639 100644
--- a/activesupport/lib/active_support/ordered_hash.rb
+++ b/activesupport/lib/active_support/ordered_hash.rb
@@ -4,62 +4,85 @@ module ActiveSupport
if RUBY_VERSION >= '1.9'
OrderedHash = ::Hash
else
- class OrderedHash < Array #:nodoc:
+ class OrderedHash < Hash #:nodoc:
+ def initialize(*args, &block)
+ super
+ @keys = []
+ end
+
def []=(key, value)
- if pair = assoc(key)
- pair.pop
- pair << value
- else
- self << [key, value]
+ if !has_key?(key)
+ @keys << key
end
- value
+ super
end
- def [](key)
- pair = assoc(key)
- pair ? pair.last : nil
+ def delete(key)
+ if has_key? key
+ index = @keys.index(key)
+ @keys.delete_at index
+ end
+ super
end
- def delete(key)
- pair = assoc(key)
- pair ? array_index = index(pair) : nil
- array_index ? delete_at(array_index).last : nil
+ def reject!
+ super
+ sync_keys!
+ self
+ end
+
+ def reject(&block)
+ dup.reject!(&block)
end
def keys
- collect { |key, value| key }
+ @keys
end
def values
- collect { |key, value| value }
+ @keys.collect { |key| self[key] }
end
def to_hash
- returning({}) do |hash|
- each { |array| hash[array[0]] = array[1] }
- end
+ Hash.new(self)
+ end
+
+ def each_key
+ @keys.each { |key| yield key }
+ end
+
+ def each_value
+ @keys.each { |key| yield self[key]}
end
- def has_key?(k)
- !assoc(k).nil?
+ def each
+ keys.each {|key| yield [key, self[key]]}
end
- alias_method :key?, :has_key?
- alias_method :include?, :has_key?
- alias_method :member?, :has_key?
+ alias_method :each_pair, :each
- def has_value?(v)
- any? { |key, value| value == v }
+ def clear
+ super
+ @keys.clear
+ self
end
- alias_method :value?, :has_value?
+ def shift
+ k = @keys.first
+ v = delete(k)
+ [k, v]
+ end
- def each_key
- each { |key, value| yield key }
+ def merge(other_hash)
+ result = dup
+ other_hash.each {|k,v| result[k]=v}
+ result
end
- def each_value
- each { |key, value| yield value }
+ private
+
+ def sync_keys!
+ @keys.delete_if {|k| !has_key?(k)}
end
end
end