diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-12-17 00:49:41 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-12-17 00:49:41 +0000 |
commit | 86deb270953f9c5b62813d3e1938f33cc807bd7f (patch) | |
tree | b057cb0c34d15e3242f07a7b2fe54f9a9e1c16f3 /activesupport/lib/active_support/core_ext | |
parent | 05a5209f823f18e423e320f6bce4a91532c8bd03 (diff) | |
download | rails-86deb270953f9c5b62813d3e1938f33cc807bd7f.tar.gz rails-86deb270953f9c5b62813d3e1938f33cc807bd7f.tar.bz2 rails-86deb270953f9c5b62813d3e1938f33cc807bd7f.zip |
Hash#slice(*keys) returns a new hash with only the given keys. #slice! replaces the hash with only the given keys. Works with HashWithIndifferentAccess also.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5726 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash.rb | 9 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/slice.rb | 28 |
2 files changed, 32 insertions, 5 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash.rb b/activesupport/lib/active_support/core_ext/hash.rb index 7d94d70910..2a99468b4e 100644 --- a/activesupport/lib/active_support/core_ext/hash.rb +++ b/activesupport/lib/active_support/core_ext/hash.rb @@ -1,8 +1,6 @@ -require File.dirname(__FILE__) + '/hash/keys' -require File.dirname(__FILE__) + '/hash/indifferent_access' -require File.dirname(__FILE__) + '/hash/reverse_merge' -require File.dirname(__FILE__) + '/hash/conversions' -require File.dirname(__FILE__) + '/hash/diff' +%w(keys indifferent_access reverse_merge conversions diff slice).each do |ext| + require "#{File.dirname(__FILE__)}/hash/#{ext}" +end class Hash #:nodoc: include ActiveSupport::CoreExtensions::Hash::Keys @@ -10,4 +8,5 @@ class Hash #:nodoc: include ActiveSupport::CoreExtensions::Hash::ReverseMerge include ActiveSupport::CoreExtensions::Hash::Conversions include ActiveSupport::CoreExtensions::Hash::Diff + include ActiveSupport::CoreExtensions::Hash::Slice end diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb new file mode 100644 index 0000000000..6fe5e05330 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -0,0 +1,28 @@ +require 'set' + +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module Hash #:nodoc: + # Slice a hash to include only the given keys. This is useful for + # limiting an options hash to valid keys before passing to a method: + # + # def search(criteria = {}) + # assert_valid_keys(:mass, :velocity, :time) + # end + # + # search(options.slice(:mass, :velocity, :time)) + module Slice + # Returns a new hash with only the given keys. + def slice(*keys) + allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys) + reject { |key,| !allowed.include?(key) } + end + + # Replaces the hash with only the given keys. + def slice!(*keys) + replace(slice(*keys)) + end + end + end + end +end |