From 300cb88134742576df917d056912e1b0f1645971 Mon Sep 17 00:00:00 2001 From: kenner kliemann Date: Wed, 30 Sep 2020 22:48:54 -0300 Subject: [PATCH 1/3] add hash slice using IndifferentAccess #529 --- CHANGELOG.md | 1 + lib/hashie/extensions/indifferent_access.rb | 5 +++++ spec/hashie/extensions/indifferent_access_spec.rb | 10 ++++++++++ 3 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2b77b79..6ef73133 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 f8176f5f..825609e7 100644 --- a/lib/hashie/extensions/indifferent_access.rb +++ b/lib/hashie/extensions/indifferent_access.rb @@ -141,6 +141,11 @@ def merge!(*) super.convert! end + def slice(*keys) + string_keys = keys.map { |key| convert_key(key) } + super(*string_keys) + 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 612f2760..f533785d 100644 --- a/spec/hashie/extensions/indifferent_access_spec.rb +++ b/spec/hashie/extensions/indifferent_access_spec.rb @@ -315,6 +315,16 @@ def indifferent_writer(key, value) end end end + + 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 describe 'with merge initializer' do From fc87461718add8e66c0d5e70be3745f1def98847 Mon Sep 17 00:00:00 2001 From: kenner kliemann Date: Fri, 2 Oct 2020 23:13:34 -0300 Subject: [PATCH 2/3] wrap slice with with_minimum_ruby --- lib/hashie/extensions/indifferent_access.rb | 10 +++++++--- .../hashie/extensions/indifferent_access_spec.rb | 16 +++++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/hashie/extensions/indifferent_access.rb b/lib/hashie/extensions/indifferent_access.rb index 825609e7..ebdb0f99 100644 --- a/lib/hashie/extensions/indifferent_access.rb +++ b/lib/hashie/extensions/indifferent_access.rb @@ -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) @@ -141,9 +143,11 @@ def merge!(*) super.convert! end - def slice(*keys) - string_keys = keys.map { |key| convert_key(key) } - super(*string_keys) + with_minimum_ruby('2.5.0') do + def slice(*keys) + string_keys = keys.map { |key| convert_key(key) } + super(*string_keys) + end end protected diff --git a/spec/hashie/extensions/indifferent_access_spec.rb b/spec/hashie/extensions/indifferent_access_spec.rb index f533785d..c48ad0c9 100644 --- a/spec/hashie/extensions/indifferent_access_spec.rb +++ b/spec/hashie/extensions/indifferent_access_spec.rb @@ -316,13 +316,15 @@ def indifferent_writer(key, value) end end - 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 + 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 From 52b5afee1ff005362bf775af29b73a34111ec59d Mon Sep 17 00:00:00 2001 From: kenner kliemann Date: Fri, 2 Oct 2020 23:15:00 -0300 Subject: [PATCH 3/3] disable rubocop module length and up rubocop TODO --- .rubocop.yml | 3 +++ .rubocop_todo.yml | 10 +++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index a3254fb8..d35e18e8 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 4a04087c..f2cc4f76 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