Skip to content

Commit 99a15e6

Browse files
committed
Support Thor::CoreExt::HashWithIndifferentAccess#except for Rails 6.0
This PR supports `Thor::CoreExt::HashWithIndifferentAccess#except` and prevents breaking changes in Rails upgrades when using `options.except(:key)` in Thor task. When Thor is used with Rails (Active Support), the behavior changes as follows. ## With Rails 5.2 or lower ```ruby h = Thor::CoreExt::HashWithIndifferentAccess.new(foo: 1, bar: 2) h.except(:foo) #=> {"bar"=>2} ``` ## With Rails 6.0 ```ruby h = Thor::CoreExt::HashWithIndifferentAccess.new(foo: 1, bar: 2) h.except(:foo) #=> {"foo"=>1, "bar"=>2} ``` This difference behavior is due to the following changes in Rails 6.0. rails/rails#35771 This PR makes the behavior between Rails 5.2 and Rails 6.0 compatible.
1 parent 34df888 commit 99a15e6

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/thor/core_ext/hash_with_indifferent_access.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ def delete(key)
2828
super(convert_key(key))
2929
end
3030

31+
def except(*keys)
32+
dup.tap do |hash|
33+
keys.each { |key| hash.delete(convert_key(key)) }
34+
end
35+
end
36+
3137
def fetch(key, *args)
3238
super(convert_key(key), *args)
3339
end

spec/core_ext/hash_with_indifferent_access_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@
1414
expect(@hash.delete(:foo)).to eq("bar")
1515
end
1616

17+
it "supports except" do
18+
unexcepted_hash = @hash.dup
19+
@hash.except("foo")
20+
expect(@hash).to eq(unexcepted_hash)
21+
22+
expect(@hash.except("foo")).to eq("baz" => "bee", "force" => true)
23+
expect(@hash.except("foo", "baz")).to eq("force" => true)
24+
expect(@hash.except(:foo)).to eq("baz" => "bee", "force" => true)
25+
expect(@hash.except(:foo, :baz)).to eq("force" => true)
26+
end
27+
1728
it "supports fetch" do
1829
expect(@hash.fetch("foo")).to eq("bar")
1930
expect(@hash.fetch("foo", nil)).to eq("bar")

0 commit comments

Comments
 (0)