1
0
Fork 0
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:
yuuji.yaginuma 2017-09-23 18:25:16 +09:00
parent 6c199967fc
commit 962ce60ff1
2 changed files with 14 additions and 1 deletions

View file

@ -46,7 +46,7 @@ module ActiveSupport
bangs = name_string.chomp!("!") bangs = name_string.chomp!("!")
if bangs 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 else
self[name_string] self[name_string]
end end

View file

@ -102,4 +102,17 @@ class OrderedOptionsTest < ActiveSupport::TestCase
end end
assert_raises(KeyError) { a.non_existing_key! } assert_raises(KeyError) { a.non_existing_key! }
end 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 end