diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-07-17 11:52:56 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-07-17 11:52:56 -0700 |
commit | 636e6b7138864ceb1e309939cd879e710b287f3e (patch) | |
tree | 2a36dfef02f706302a20bb5b2d428b32e9af97f0 /activesupport/lib/active_support/core_ext/hash | |
parent | 842917dea0cfdf70f158a312cc1f77f769791d8c (diff) | |
parent | 99930d499e424f4560b371412e05d10476216ece (diff) | |
download | rails-636e6b7138864ceb1e309939cd879e710b287f3e.tar.gz rails-636e6b7138864ceb1e309939cd879e710b287f3e.tar.bz2 rails-636e6b7138864ceb1e309939cd879e710b287f3e.zip |
Merge branch 'master' into i18n-merge
Conflicts:
actionpack/lib/action_view/helpers/form_options_helper.rb
activerecord/lib/active_record/validations.rb
Diffstat (limited to 'activesupport/lib/active_support/core_ext/hash')
4 files changed, 41 insertions, 6 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/deep_merge.rb b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb new file mode 100644 index 0000000000..f8842ba57a --- /dev/null +++ b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb @@ -0,0 +1,23 @@ +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module Hash #:nodoc: + # Allows for deep merging + module DeepMerge + # Returns a new hash with +self+ and +other_hash+ merged recursively. + def deep_merge(other_hash) + self.merge(other_hash) do |key, oldval, newval| + oldval = oldval.to_hash if oldval.respond_to?(:to_hash) + newval = newval.to_hash if newval.respond_to?(:to_hash) + oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ? oldval.deep_merge(newval) : newval + end + end + + # Returns a new hash with +self+ and +other_hash+ merged recursively. + # Modifies the receiver in place. + def deep_merge!(other_hash) + replace(deep_merge(other_hash)) + end + end + end + end +end diff --git a/activesupport/lib/active_support/core_ext/hash/except.rb b/activesupport/lib/active_support/core_ext/hash/except.rb index bc97fa35a6..f26d01553d 100644 --- a/activesupport/lib/active_support/core_ext/hash/except.rb +++ b/activesupport/lib/active_support/core_ext/hash/except.rb @@ -13,7 +13,7 @@ module ActiveSupport #:nodoc: clone.except!(*keys) end - # Replaces the hash without only the given keys. + # Replaces the hash without the given keys. def except!(*keys) keys.map! { |key| convert_key(key) } if respond_to?(:convert_key) keys.each { |key| delete(key) } diff --git a/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb b/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb index 7af10846e7..546e261cc9 100644 --- a/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb +++ b/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb @@ -1,21 +1,28 @@ module ActiveSupport #:nodoc: module CoreExtensions #:nodoc: module Hash #:nodoc: - # Allows for reverse merging where its the keys in the calling hash that wins over those in the <tt>other_hash</tt>. - # This is particularly useful for initializing an incoming option hash with default values: + # Allows for reverse merging two hashes where the keys in the calling hash take precedence over those + # in the <tt>other_hash</tt>. This is particularly useful for initializing an option hash with default values: # # def setup(options = {}) # options.reverse_merge! :size => 25, :velocity => 10 # end # - # The default <tt>:size</tt> and <tt>:velocity</tt> is only set if the +options+ passed in doesn't already have those keys set. + # Using <tt>merge</tt>, the above example would look as follows: + # + # def setup(options = {}) + # { :size => 25, :velocity => 10 }.merge(options) + # end + # + # The default <tt>:size</tt> and <tt>:velocity</tt> are only set if the +options+ hash passed in doesn't already + # have the respective key. module ReverseMerge - # Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second. + # Performs the opposite of <tt>merge</tt>, with the keys and values from the first hash taking precedence over the second. def reverse_merge(other_hash) other_hash.merge(self) end - # Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second. + # Performs the opposite of <tt>merge</tt>, with the keys and values from the first hash taking precedence over the second. # Modifies the receiver in place. def reverse_merge!(other_hash) replace(reverse_merge(other_hash)) diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb index be4dec6e53..3f14470f36 100644 --- a/activesupport/lib/active_support/core_ext/hash/slice.rb +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -9,6 +9,11 @@ module ActiveSupport #:nodoc: # end # # search(options.slice(:mass, :velocity, :time)) + # + # If you have an array of keys you want to limit to, you should splat them: + # + # valid_keys = [:mass, :velocity, :time] + # search(options.slice(*valid_keys)) module Slice # Returns a new hash with only the given keys. def slice(*keys) |