diff options
| author | jonathan schatz <jon@instamotor.com> | 2016-05-31 15:53:33 -0700 |
|---|---|---|
| committer | jonathan schatz <jon@instamotor.com> | 2016-06-01 14:53:29 -0700 |
| commit | fa524096f33ebfb7ffadaf3594dddda5fd69ffb4 (patch) | |
| tree | 0c4e09c512b3191b0e5a6e2bd94c9861ccdc4c76 /lib | |
| parent | fb186400247779f90aacf23fa2cde044cbac387c (diff) | |
| download | hashie-fa524096f33ebfb7ffadaf3594dddda5fd69ffb4.tar.gz | |
Fix support for Array#dig
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/hashie.rb | 6 | ||||
| -rw-r--r-- | lib/hashie/array.rb | 11 | ||||
| -rw-r--r-- | lib/hashie/extensions/array/pretty_inspect.rb | 19 | ||||
| -rw-r--r-- | lib/hashie/extensions/ruby_version_check.rb | 15 | ||||
| -rw-r--r-- | lib/hashie/mash.rb | 6 |
5 files changed, 56 insertions, 1 deletions
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 |
