Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #except under Ruby 3 #545

Merged
merged 2 commits into from Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -40,7 +40,7 @@ Any violations of this scheme are considered to be bugs.

* [#516](https://github.com/hashie/hashie/issues/516): Fixed `NoMethodError` raised when including `Hashie::Extensions::Mash::SymbolizeKeys` and `Hashie::Extensions::SymbolizeKeys` in mashes/hashes with non string or symbol keys - [@carolineartz](https://github.com/carolineartz).
* [#531](https://github.com/hashie/hashie/pull/531): Fixed [slice doesn't work using symbols](https://github.com/hashie/hashie/issues/529) using hash with `IndifferentAccess` extension - [@gnomex](https://github.com/gnomex).
* [#533](https://github.com/hashie/hashie/pull/533): Fixed `NoMethodError: undefined method `to_json'` at `hashie/dash_spec` - [@gnomex](https://github.com/gnomex).
* [#533](https://github.com/hashie/hashie/pull/533): Fixed `NoMethodError: undefined method 'to_json'` at `hashie/dash_spec` - [@gnomex](https://github.com/gnomex).
* [#537](https://github.com/hashie/hashie/pull/537): Fixed inconsistencies with handling defaults in `Dash` with and without `IgnoreUnclared` mixed in - [@michaelherold](https://github.com/michaelherold).
* Your contribution here.

Expand All @@ -54,6 +54,7 @@ Any violations of this scheme are considered to be bugs.

### Added

* [#545](https://github.com/hashie/hashie/pull/545): Add `Hashie::Mash#except` and `Hashie::Extensions::IndifferentAccess#except` when running under Ruby 3 to match newly added Ruby stdlib method - [@jackjennings](https://github.com/jackjennings).
* [#499](https://github.com/hashie/hashie/pull/499): Add `Hashie::Extensions::Mash::PermissiveRespondTo` to make specific subclasses of Mash fully respond to messages for use with `SimpleDelegator` - [@michaelherold](https://github.com/michaelherold).

### Changed
Expand Down
7 changes: 7 additions & 0 deletions lib/hashie/extensions/indifferent_access.rb
Expand Up @@ -162,6 +162,13 @@ def slice(*keys)
end
end

with_minimum_ruby('3.0.0') do
def except(*keys)
string_keys = keys.map { |key| convert_key(key) }
super(*string_keys)
end
end

protected

def hash_lacking_indifference?(other)
Expand Down
7 changes: 7 additions & 0 deletions lib/hashie/mash.rb
Expand Up @@ -351,6 +351,13 @@ def transform_keys(&blk)
end
end

with_minimum_ruby('3.0.0') do
def except(*keys)
string_keys = keys.map { |key| convert_key(key) }
self.class.new(super(*string_keys))
end
end

protected

def method_name_and_suffix(method_name)
Expand Down
12 changes: 12 additions & 0 deletions spec/hashie/extensions/indifferent_access_spec.rb
Expand Up @@ -357,6 +357,18 @@ def indifferent_writer(key, value)
end
end
end

with_minimum_ruby('3.0.0') do
describe '#except' do
let(:h) { subject.build(foo: 'bar', baz: 'qux') }

it 'indifferently excepts keys from the hash' do
sliced_h = { 'baz' => 'qux' }
expect(h.except('foo')).to eq sliced_h
expect(h.except(:foo)).to eq sliced_h
end
end
end
end

describe 'with merge initializer' do
Expand Down
13 changes: 13 additions & 0 deletions spec/hashie/mash_spec.rb
Expand Up @@ -1097,4 +1097,17 @@ class SubMash < Hashie::Mash
end
end
end

with_minimum_ruby('3.0.0') do
context '#except' do
subject(:mash) { described_class.new(a: 'A', b: 'B') }
it 'return a Hashie::Mash' do
expect(mash.except(:b)).to be_kind_of(described_class)
end

it 'excludes keys' do
expect(mash.except(:b)).to eq('a' => 'A')
end
end
end
end