Add hash slice using IndifferentAccess.

This commit is contained in:
kenner kliemann 2020-10-03 08:57:11 -04:00 committed by dblock
parent a30327a40b
commit 792714b91c
5 changed files with 30 additions and 5 deletions

View File

@ -15,6 +15,9 @@ Layout/IndentHeredoc:
Metrics/ClassLength:
Enabled: false
Metrics/ModuleLength:
Enabled: false
Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'

View File

@ -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
@ -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

View File

@ -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).
* Your contribution here.
### Security

View File

@ -23,6 +23,8 @@ module Hashie
# h['baz'] # => 'blip'
#
module IndifferentAccess
include Hashie::Extensions::RubyVersionCheck
def self.included(base)
Hashie::Extensions::Dash::IndifferentAccess.maybe_extend(base)
@ -141,6 +143,13 @@ module Hashie
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)

View File

@ -315,6 +315,18 @@ describe Hashie::Extensions::IndifferentAccess do
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