diff options
author | Michael Koziarski <michael@koziarski.com> | 2007-10-08 03:30:51 +0000 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2007-10-08 03:30:51 +0000 |
commit | b3d9e2372b63d6273d73765231e9849bafee178b (patch) | |
tree | b47cc54a5721b4ea25641e94c676472a5f0f477c | |
parent | 074fe35b8a9c82e22a2c8b7559df158ad513981a (diff) | |
download | rails-b3d9e2372b63d6273d73765231e9849bafee178b.tar.gz rails-b3d9e2372b63d6273d73765231e9849bafee178b.tar.bz2 rails-b3d9e2372b63d6273d73765231e9849bafee178b.zip |
Documentation for assert_valid_keys. Closes #7264 [tarmo, rsanheim]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7792 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/keys.rb | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb index 3c8a59f00a..a9169cf4a5 100644 --- a/activesupport/lib/active_support/core_ext/hash/keys.rb +++ b/activesupport/lib/active_support/core_ext/hash/keys.rb @@ -43,6 +43,13 @@ module ActiveSupport #:nodoc: alias_method :to_options, :symbolize_keys alias_method :to_options!, :symbolize_keys! + # Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch. + # Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbol + # as keys, this will fail. + # examples: + # { :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key(s): years" + # { :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key(s): years, name" + # { :name => "Rob", :age => "28" }.assert_valid_keys(:name, :age) # => passes, raises nothing def assert_valid_keys(*valid_keys) unknown_keys = keys - [valid_keys].flatten raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty? |