aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2007-07-09 21:49:37 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2007-07-09 21:49:37 +0000
commitcb2381696029ad839ecddad08afea061d07685fb (patch)
tree019bdde1381a1fd2e9423c494c13d76a048e4366 /activesupport/lib
parent5b2be6293443a4c25aab7cf3e74fc84cb70e2199 (diff)
downloadrails-cb2381696029ad839ecddad08afea061d07685fb.tar.gz
rails-cb2381696029ad839ecddad08afea061d07685fb.tar.bz2
rails-cb2381696029ad839ecddad08afea061d07685fb.zip
Added Hash#except which is the inverse of Hash#slice -- return the hash except the keys that are specified [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7172 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/core_ext/hash.rb3
-rw-r--r--activesupport/lib/active_support/core_ext/hash/except.rb24
2 files changed, 26 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash.rb b/activesupport/lib/active_support/core_ext/hash.rb
index 2a99468b4e..b805d2da62 100644
--- a/activesupport/lib/active_support/core_ext/hash.rb
+++ b/activesupport/lib/active_support/core_ext/hash.rb
@@ -1,4 +1,4 @@
-%w(keys indifferent_access reverse_merge conversions diff slice).each do |ext|
+%w(keys indifferent_access reverse_merge conversions diff slice except).each do |ext|
require "#{File.dirname(__FILE__)}/hash/#{ext}"
end
@@ -9,4 +9,5 @@ class Hash #:nodoc:
include ActiveSupport::CoreExtensions::Hash::Conversions
include ActiveSupport::CoreExtensions::Hash::Diff
include ActiveSupport::CoreExtensions::Hash::Slice
+ include ActiveSupport::CoreExtensions::Hash::Except
end
diff --git a/activesupport/lib/active_support/core_ext/hash/except.rb b/activesupport/lib/active_support/core_ext/hash/except.rb
new file mode 100644
index 0000000000..8362cd880e
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/hash/except.rb
@@ -0,0 +1,24 @@
+require 'set'
+
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module Hash #:nodoc:
+ # Return a hash that includes everything but the given keys. This is useful for
+ # limiting a set of parameters to everything but a few known toggles:
+ #
+ # @person.update_attributes(params[:person].except(:admin))
+ module Except
+ # Returns a new hash without the given keys.
+ def except(*keys)
+ rejected = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
+ reject { |key,| rejected.include?(key) }
+ end
+
+ # Replaces the hash without only the given keys.
+ def except!(*keys)
+ replace(except(*keys))
+ end
+ end
+ end
+ end
+end