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 hash slice using IndifferentAccess #531

Closed
wants to merge 3 commits into from
Closed
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: 3 additions & 0 deletions .rubocop.yml
Expand Up @@ -15,6 +15,9 @@ Layout/IndentHeredoc:
Metrics/ClassLength:
Enabled: false

Metrics/ModuleLength:
Enabled: false

Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'
Expand Down
10 changes: 5 additions & 5 deletions .rubocop_todo.yml
@@ -1,16 +1,16 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-10-10 00:07:29 -0400 using RuboCop version 0.52.1.
# on 2020-10-02 23:12:27 -0300 using RuboCop version 0.52.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 7
# Offense count: 9
Metrics/AbcSize:
Max: 23

# Offense count: 7
# Offense count: 8
Metrics/CyclomaticComplexity:
Max: 11

Expand All @@ -19,10 +19,10 @@ Metrics/CyclomaticComplexity:
Metrics/MethodLength:
Max: 28

# Offense count: 5
# Offense count: 6
Metrics/PerceivedComplexity:
Max: 10

# Offense count: 41
# Offense count: 44
Style/Documentation:
Enabled: false
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -36,6 +36,7 @@ Any violations of this scheme are considered to be bugs.
### Fixed

* [#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)
Copy link
Member

@dblock dblock Oct 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing a period in the end to match other lines, please.

* Your contribution here.

### Security
Expand Down
9 changes: 9 additions & 0 deletions lib/hashie/extensions/indifferent_access.rb
Expand Up @@ -23,6 +23,8 @@ module Extensions
# h['baz'] # => 'blip'
#
module IndifferentAccess
include Hashie::Extensions::RubyVersionCheck

def self.included(base)
Hashie::Extensions::Dash::IndifferentAccess.maybe_extend(base)

Expand Down Expand Up @@ -141,6 +143,13 @@ def merge!(*)
super.convert!
end

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

protected

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

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

it 'indifferently slices the hash' do
sliced_h = { 'foo' => 'bar' }
expect(h.slice('foo')).to eq sliced_h
expect(h.slice(:foo)).to eq sliced_h
end
end
end
end

describe 'with merge initializer' do
Expand Down