mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make bang version work with InheritableOptions
Currently, bang version does not work with `InheritableOptions`. `InheritableOptions` treats the argument Hash as the default value. However, `Hash#fetch` does not use the default value when key is not found, so can not get the default value. So in bang version, should use `Hash#[]` instead of `Hash#fetch`.
This commit is contained in:
parent
6c199967fc
commit
962ce60ff1
2 changed files with 14 additions and 1 deletions
|
@ -46,7 +46,7 @@ module ActiveSupport
|
|||
bangs = name_string.chomp!("!")
|
||||
|
||||
if bangs
|
||||
fetch(name_string.to_sym).presence || raise(KeyError.new(":#{name_string} is blank"))
|
||||
self[name_string].presence || raise(KeyError.new(":#{name_string} is blank"))
|
||||
else
|
||||
self[name_string]
|
||||
end
|
||||
|
|
|
@ -102,4 +102,17 @@ class OrderedOptionsTest < ActiveSupport::TestCase
|
|||
end
|
||||
assert_raises(KeyError) { a.non_existing_key! }
|
||||
end
|
||||
|
||||
def test_inheritable_options_with_bang
|
||||
a = ActiveSupport::InheritableOptions.new(foo: :bar)
|
||||
|
||||
assert_nothing_raised { a.foo! }
|
||||
assert_equal a.foo, a.foo!
|
||||
|
||||
assert_raises(KeyError) do
|
||||
a.foo = nil
|
||||
a.foo!
|
||||
end
|
||||
assert_raises(KeyError) { a.non_existing_key! }
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue