From 452cd74d81ecfa38d0c7d3a4dc83d2b731de9e73 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Sun, 4 Jan 2009 14:01:23 +0000 Subject: Dup keys in OrderedHash to prevent them from being modified [#1676 state:resolved] Signed-off-by: Frederick Cheung --- activesupport/lib/active_support/ordered_hash.rb | 31 +++++++++++++++++------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'activesupport/lib/active_support/ordered_hash.rb') 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)} -- cgit v1.2.3 From f4bf318db061daa8c6ebb2b700fd15034e031310 Mon Sep 17 00:00:00 2001 From: Greg Borenstein Date: Mon, 19 Jan 2009 11:44:55 -0800 Subject: add an inspect method to OrderedHash to make it clear that it is not a species of Array Signed-off-by: Michael Koziarski [#1782 state:committed] --- activesupport/lib/active_support/ordered_hash.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activesupport/lib/active_support/ordered_hash.rb') diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb index 25ea505813..66aab9e562 100644 --- a/activesupport/lib/active_support/ordered_hash.rb +++ b/activesupport/lib/active_support/ordered_hash.rb @@ -92,6 +92,10 @@ module ActiveSupport dup.merge!(other_hash) end + def inspect + "#" + end + private def sync_keys! -- cgit v1.2.3