Fix support for Array#dig
This commit is contained in:
parent
fb18640024
commit
fa524096f3
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
module RubyVersionCheck
|
||||
def with_minimum_ruby(version)
|
||||
yield if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new(version)
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue