From 04e8a1960e4d2204c0081abc272e8478cda3f780 Mon Sep 17 00:00:00 2001 From: Jack Jennings Date: Tue, 6 Apr 2021 17:45:02 -0700 Subject: [PATCH] Add #except under Ruby 3 Ruby 3 adds the Hash#except method, which should return the correct Hashie object instance when called on a Mash or object using the Hashie::Extensions::IndifferentAccess mixin. --- CHANGELOG.md | 1 + lib/hashie/extensions/indifferent_access.rb | 7 +++++++ lib/hashie/mash.rb | 7 +++++++ spec/hashie/extensions/indifferent_access_spec.rb | 12 ++++++++++++ spec/hashie/mash_spec.rb | 13 +++++++++++++ 5 files changed, 40 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0db8b66d..a9af09f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/hashie/extensions/indifferent_access.rb b/lib/hashie/extensions/indifferent_access.rb index d6b9da33..c2c78888 100644 --- a/lib/hashie/extensions/indifferent_access.rb +++ b/lib/hashie/extensions/indifferent_access.rb @@ -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) diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb index f72633d2..d5cb3084 100644 --- a/lib/hashie/mash.rb +++ b/lib/hashie/mash.rb @@ -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) diff --git a/spec/hashie/extensions/indifferent_access_spec.rb b/spec/hashie/extensions/indifferent_access_spec.rb index 0aec787f..27585f11 100644 --- a/spec/hashie/extensions/indifferent_access_spec.rb +++ b/spec/hashie/extensions/indifferent_access_spec.rb @@ -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 diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb index 4731985c..84a28ead 100644 --- a/spec/hashie/mash_spec.rb +++ b/spec/hashie/mash_spec.rb @@ -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