diff --git a/.rubocop.yml b/.rubocop.yml index a3254fb..d35e18e 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -15,6 +15,9 @@ Layout/IndentHeredoc: Metrics/ClassLength: Enabled: false +Metrics/ModuleLength: + Enabled: false + Metrics/BlockLength: Exclude: - 'spec/**/*.rb' diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4a04087..f2cc4f7 100644 --- a/.rubocop_todo.yml +++ b/.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 @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index a2b77b7..1800fec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/hashie/extensions/indifferent_access.rb b/lib/hashie/extensions/indifferent_access.rb index f8176f5..ebdb0f9 100644 --- a/lib/hashie/extensions/indifferent_access.rb +++ b/lib/hashie/extensions/indifferent_access.rb @@ -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) diff --git a/spec/hashie/extensions/indifferent_access_spec.rb b/spec/hashie/extensions/indifferent_access_spec.rb index 612f276..c48ad0c 100644 --- a/spec/hashie/extensions/indifferent_access_spec.rb +++ b/spec/hashie/extensions/indifferent_access_spec.rb @@ -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