diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-03-20 03:57:13 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-03-20 03:57:13 +0000 |
commit | 324ece25e9c9fb13ce91e591d5c5fdd512a81bb0 (patch) | |
tree | 4777713c98920f45a06eb35223dd2eaef3d42592 /activesupport/lib | |
parent | afde9943eaec9c7370c2638316128e236222df82 (diff) | |
download | rails-324ece25e9c9fb13ce91e591d5c5fdd512a81bb0.tar.gz rails-324ece25e9c9fb13ce91e591d5c5fdd512a81bb0.tar.bz2 rails-324ece25e9c9fb13ce91e591d5c5fdd512a81bb0.zip |
Rolled back #2176 because it destroyed performance [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3988 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/indifferent_access.rb | 60 |
1 files changed, 36 insertions, 24 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb index 4b35893e20..241506b4b8 100644 --- a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb +++ b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb @@ -8,43 +8,55 @@ class HashWithIndifferentAccess < Hash super(constructor) end end + + def default(key) + self[key.to_s] if key.is_a?(Symbol) + end - %w( [] []= fetch store delete has_key? include? key? member? ).each do |method_name| - class_eval %( - def #{method_name}(key, *args) - super(convert_key(key), *args.map { |arg| convert_value(arg) }) - end - ) + alias_method :regular_writer, :[]= unless method_defined?(:regular_writer) + alias_method :regular_update, :update unless method_defined?(:regular_update) + + def []=(key, value) + regular_writer(convert_key(key), convert_value(value)) end - #define_method method_name, &convert_key_and_hashes_and_call_super } - %w( == eql? replace initialize_copy merge merge! update ).each do |method_name| - class_eval %( - def #{method_name}(other_hash) - super(convert_hash(other_hash)) - end - ) + + def update(other_hash) + other_hash.each_pair {|key, value| regular_writer(convert_key(key), convert_value(value))} + self end + alias_method :merge!, :update - def invert - self.class.new.replace(super) + def key?(key) + super(convert_key(key)) end - - def values_at(*keys) - super *keys.map{ |key| convert_key(key) } + + alias_method :include?, :key? + alias_method :has_key?, :key? + alias_method :member?, :key? + + def fetch(key, *extras) + super(convert_key(key), *extras) end + def values_at(*indices) + indices.collect {|key| self[convert_key(key)]} + end + + def dup + HashWithIndifferentAccess.new(self) + end + + def merge(hash) + self.dup.update(hash) + end + protected def convert_key(key) key.kind_of?(Symbol) ? key.to_s : key end - def convert_value(value) value.is_a?(Hash) ? value.with_indifferent_access : value end - - def convert_hash(hash) - hash.is_a?(Hash) ? hash.inject({}){ |h,(k,v)| h[convert_key(k)] = convert_value(v); h } : hash - end end module ActiveSupport #:nodoc: @@ -57,4 +69,4 @@ module ActiveSupport #:nodoc: end end end -end +end
\ No newline at end of file |