Skip to content

Commit

Permalink
Merge pull request #812 from shuuuuun/hash-support-slice
Browse files Browse the repository at this point in the history
Support `Thor::CoreExt::HashWithIndifferentAccess#slice` method
  • Loading branch information
rafaelfranca authored May 15, 2023
2 parents c3f77d5 + 4bd417b commit d8b073c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/thor/core_ext/hash_with_indifferent_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def fetch(key, *args)
super(convert_key(key), *args)
end

def slice(*keys)
super(*keys.map{ |key| convert_key(key) })
end

def key?(key)
super(convert_key(key))
end
Expand Down
14 changes: 14 additions & 0 deletions spec/core_ext/hash_with_indifferent_access_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@
expect(@hash.fetch(:missing, :found)).to eq(:found)
end

it "supports slice" do
expect(@hash.slice("foo")).to eq({"foo" => "bar"})
expect(@hash.slice(:foo)).to eq({"foo" => "bar"})

expect(@hash.slice("baz")).to eq({"baz" => "bee"})
expect(@hash.slice(:baz)).to eq({"baz" => "bee"})

expect(@hash.slice("foo", "baz")).to eq({"foo" => "bar", "baz" => "bee"})
expect(@hash.slice(:foo, :baz)).to eq({"foo" => "bar", "baz" => "bee"})

expect(@hash.slice("missing")).to eq({})
expect(@hash.slice(:missing)).to eq({})
end

it "has key checkable by either strings or symbols" do
expect(@hash.key?("foo")).to be true
expect(@hash.key?(:foo)).to be true
Expand Down

0 comments on commit d8b073c

Please sign in to comment.