mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make assert_valid_keys slightly faster.
This commit is contained in:
parent
4c360c15e5
commit
77efc20a54
2 changed files with 7 additions and 5 deletions
|
@ -35,11 +35,13 @@ class Hash
|
|||
# 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): name, age"
|
||||
# { :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: years"
|
||||
# { :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key: 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?
|
||||
valid_keys.flatten!
|
||||
each_key do |k|
|
||||
raise(ArgumentError, "Unknown key: #{k}") unless valid_keys.include?(k)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -282,7 +282,7 @@ class HashExtTest < Test::Unit::TestCase
|
|||
{ :failure => "stuff", :funny => "business" }.assert_valid_keys(:failure, :funny)
|
||||
end
|
||||
|
||||
assert_raise(ArgumentError, "Unknown key(s): failore") do
|
||||
assert_raise(ArgumentError, "Unknown key: failore") do
|
||||
{ :failore => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])
|
||||
{ :failore => "stuff", :funny => "business" }.assert_valid_keys(:failure, :funny)
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue