diff options
author | Brandon Keepers <brandon@collectiveidea.com> | 2009-01-04 14:01:23 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-01-16 17:23:43 +0000 |
commit | 452cd74d81ecfa38d0c7d3a4dc83d2b731de9e73 (patch) | |
tree | ded1aa6715a13656e79b40ad054a863fb2bed315 /activesupport/lib/active_support | |
parent | 5ed119c005864b586a259e9d8def5f7aef8a4e54 (diff) | |
download | rails-452cd74d81ecfa38d0c7d3a4dc83d2b731de9e73.tar.gz rails-452cd74d81ecfa38d0c7d3a4dc83d2b731de9e73.tar.bz2 rails-452cd74d81ecfa38d0c7d3a4dc83d2b731de9e73.zip |
Dup keys in OrderedHash to prevent them from being modified [#1676 state:resolved]
Signed-off-by: Frederick Cheung <frederick.cheung@gmail.com>
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/ordered_hash.rb | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb index 3def0be639..25ea505813 100644 --- a/activesupport/lib/active_support/ordered_hash.rb +++ b/activesupport/lib/active_support/ordered_hash.rb @@ -10,10 +10,14 @@ module ActiveSupport @keys = [] end + def initialize_copy(other) + super + # make a deep copy of keys + @keys = other.keys + end + def []=(key, value) - if !has_key?(key) - @keys << key - end + @keys << key if !has_key?(key) super end @@ -24,6 +28,12 @@ module ActiveSupport end super end + + def delete_if + super + sync_keys! + self + end def reject! super @@ -36,7 +46,7 @@ module ActiveSupport end def keys - @keys + @keys.dup end def values @@ -56,7 +66,7 @@ module ActiveSupport end def each - keys.each {|key| yield [key, self[key]]} + @keys.each {|key| yield [key, self[key]]} end alias_method :each_pair, :each @@ -73,13 +83,16 @@ module ActiveSupport [k, v] end + def merge!(other_hash) + other_hash.each {|k,v| self[k] = v } + self + end + def merge(other_hash) - result = dup - other_hash.each {|k,v| result[k]=v} - result + dup.merge!(other_hash) end - private + private def sync_keys! @keys.delete_if {|k| !has_key?(k)} |