summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org>2016-06-02 07:00:38 -0400
committerDaniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org>2016-06-02 07:00:38 -0400
commitcfd64956294de8f1731ccaaa98349ca3e0311272 (patch)
tree0c4e09c512b3191b0e5a6e2bd94c9861ccdc4c76
parentfb186400247779f90aacf23fa2cde044cbac387c (diff)
parentfa524096f33ebfb7ffadaf3594dddda5fd69ffb4 (diff)
downloadhashie-cfd64956294de8f1731ccaaa98349ca3e0311272.tar.gz
Merge pull request #358 from modosc/fix-array-dig
Fix support for Array#dig
-rw-r--r--.rubocop_todo.yml14
-rw-r--r--CHANGELOG.md2
-rw-r--r--lib/hashie.rb6
-rw-r--r--lib/hashie/array.rb11
-rw-r--r--lib/hashie/extensions/array/pretty_inspect.rb19
-rw-r--r--lib/hashie/extensions/ruby_version_check.rb15
-rw-r--r--lib/hashie/mash.rb6
-rw-r--r--spec/hashie/array_spec.rb17
-rw-r--r--spec/hashie/mash_spec.rb11
-rw-r--r--spec/spec_helper.rb2
-rw-r--r--spec/support/ruby_version_check.rb5
11 files changed, 97 insertions, 11 deletions
diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml
index e027ee0..a141524 100644
--- a/.rubocop_todo.yml
+++ b/.rubocop_todo.yml
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
-# on 2015-10-25 15:28:03 -0400 using RuboCop version 0.34.2.
+# on 2016-06-01 14:51:33 -0700 using RuboCop version 0.34.2.
# 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
@@ -18,23 +18,23 @@ Metrics/AbcSize:
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
- Max: 176
+ Max: 179
# Offense count: 6
Metrics/CyclomaticComplexity:
Max: 11
-# Offense count: 218
+# Offense count: 231
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 170
-# Offense count: 17
+# Offense count: 16
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 28
-# Offense count: 6
+# Offense count: 5
Metrics/PerceivedComplexity:
Max: 10
@@ -43,7 +43,7 @@ Style/CaseEquality:
Exclude:
- 'lib/hashie/hash.rb'
-# Offense count: 27
+# Offense count: 32
# Configuration parameters: Exclude.
Style/Documentation:
Enabled: false
@@ -57,7 +57,7 @@ Style/DoubleNegation:
- 'lib/hashie/mash.rb'
- 'spec/hashie/extensions/coercion_spec.rb'
-# Offense count: 3
+# Offense count: 5
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues.
Style/HashSyntax:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0bb43a6..42e2a38 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,7 +28,7 @@ scheme are considered to be bugs.
### Fixed
-* Nothing yet.
+* [#358](https://github.com/intridea/hashie/pull/358): Fix support for Array#dig - [@modosc](https://github.com/modosc/).
### Security
diff --git a/lib/hashie.rb b/lib/hashie.rb
index 59a7c25..5346b61 100644
--- a/lib/hashie.rb
+++ b/lib/hashie.rb
@@ -7,6 +7,7 @@ module Hashie
autoload :Mash, 'hashie/mash'
autoload :Trash, 'hashie/trash'
autoload :Rash, 'hashie/rash'
+ autoload :Array, 'hashie/array'
module Extensions
autoload :Coercion, 'hashie/extensions/coercion'
@@ -27,6 +28,7 @@ module Hashie
autoload :KeyConversion, 'hashie/extensions/key_conversion'
autoload :MethodAccessWithOverride, 'hashie/extensions/method_access'
autoload :StrictKeyAccess, 'hashie/extensions/strict_key_access'
+ autoload :RubyVersionCheck, 'hashie/extensions/ruby_version_check'
module Parsers
autoload :YamlErbParser, 'hashie/extensions/parsers/yaml_erb_parser'
@@ -41,6 +43,10 @@ module Hashie
module Mash
autoload :SafeAssignment, 'hashie/extensions/mash/safe_assignment'
end
+
+ module Array
+ autoload :PrettyInspect, 'hashie/extensions/array/pretty_inspect'
+ end
end
class << self
diff --git a/lib/hashie/array.rb b/lib/hashie/array.rb
new file mode 100644
index 0000000..b0dca8b
--- /dev/null
+++ b/lib/hashie/array.rb
@@ -0,0 +1,11 @@
+module Hashie
+ class Array < ::Array
+ include Hashie::Extensions::Array::PrettyInspect
+ include Hashie::Extensions::RubyVersionCheck
+ with_minimum_ruby('2.3.0') do
+ def dig(*indexes)
+ super(*indexes.map { |idx| Integer(idx) })
+ end
+ end
+ end
+end
diff --git a/lib/hashie/extensions/array/pretty_inspect.rb b/lib/hashie/extensions/array/pretty_inspect.rb
new file mode 100644
index 0000000..48ce119
--- /dev/null
+++ b/lib/hashie/extensions/array/pretty_inspect.rb
@@ -0,0 +1,19 @@
+module Hashie
+ module Extensions
+ module Array
+ module PrettyInspect
+ def self.included(base)
+ base.send :alias_method, :array_inspect, :inspect
+ base.send :alias_method, :inspect, :hashie_inspect
+ end
+
+ def hashie_inspect
+ ret = "#<#{self.class} ["
+ ret << to_a.map(&:inspect).join(', ')
+ ret << ']>'
+ ret
+ end
+ end
+ end
+ end
+end
diff --git a/lib/hashie/extensions/ruby_version_check.rb b/lib/hashie/extensions/ruby_version_check.rb
new file mode 100644
index 0000000..e1f70d4
--- /dev/null
+++ b/lib/hashie/extensions/ruby_version_check.rb
@@ -0,0 +1,15 @@
+module Hashie
+ module Extensions
+ module RubyVersionCheck
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ def with_minimum_ruby(version)
+ yield if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new(version)
+ end
+ end
+ end
+ end
+end
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb
index a960d3d..0cae7ea 100644
--- a/lib/hashie/mash.rb
+++ b/lib/hashie/mash.rb
@@ -1,4 +1,5 @@
require 'hashie/hash'
+require 'hashie/array'
module Hashie
# Mash allows you to create pseudo-objects that have method-like
@@ -56,6 +57,7 @@ module Hashie
#
class Mash < Hash
include Hashie::Extensions::PrettyInspect
+ include Hashie::Extensions::RubyVersionCheck
ALLOWED_SUFFIXES = %w(? ! = _)
@@ -250,7 +252,7 @@ module Hashie
self.class.new(other_hash).merge(self)
end
- if RUBY_VERSION >= '2.3.0'
+ with_minimum_ruby('2.3.0') do
def dig(*keys)
super(*keys.map { |key| convert_key(key) })
end
@@ -287,6 +289,8 @@ module Hashie
self.class.new(val)
when Array
val.map { |e| convert_value(e) }
+ when ::Array
+ Array.new(val.map { |e| convert_value(e) })
else
val
end
diff --git a/spec/hashie/array_spec.rb b/spec/hashie/array_spec.rb
new file mode 100644
index 0000000..6c20a83
--- /dev/null
+++ b/spec/hashie/array_spec.rb
@@ -0,0 +1,17 @@
+require 'spec_helper'
+
+describe Array do
+ with_minimum_ruby('2.3.0') do
+ describe '#dig' do
+ let(:array) { Hashie::Array.new([:a, :b, :c]) }
+
+ it 'works with a string index' do
+ expect(array.dig('0')).to eq(:a)
+ end
+
+ it 'works with a numeric index' do
+ expect(array.dig(1)).to eq(:b)
+ end
+ end
+ end
+end
diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb
index 9aac1ad..f187d1f 100644
--- a/spec/hashie/mash_spec.rb
+++ b/spec/hashie/mash_spec.rb
@@ -687,14 +687,21 @@ describe Hashie::Mash do
end
end
- if RUBY_VERSION >= '2.3.0'
+ with_minimum_ruby('2.3.0') do
describe '#dig' do
subject { described_class.new(a: { b: 1 }) }
-
it 'accepts both string and symbol as key' do
expect(subject.dig(:a, :b)).to eq(1)
expect(subject.dig('a', 'b')).to eq(1)
end
+
+ context 'with numeric key' do
+ subject { described_class.new('1' => { b: 1 }) }
+ it 'accepts a numeric value as key' do
+ expect(subject.dig(1, :b)).to eq(1)
+ expect(subject.dig('1', :b)).to eq(1)
+ end
+ end
end
end
end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index e1bcd13..2cd68e3 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -8,8 +8,10 @@ require 'pry'
require 'rspec'
require 'hashie'
require 'rspec/pending_for'
+require './spec/support/ruby_version_check'
RSpec.configure do |config|
+ config.extend RubyVersionCheck
config.expect_with :rspec do |expect|
expect.syntax = :expect
end
diff --git a/spec/support/ruby_version_check.rb b/spec/support/ruby_version_check.rb
new file mode 100644
index 0000000..c3890c6
--- /dev/null
+++ b/spec/support/ruby_version_check.rb
@@ -0,0 +1,5 @@
+module RubyVersionCheck
+ def with_minimum_ruby(version)
+ yield if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new(version)
+ end
+end