Sha256: 642514d72539fb9a93b7abb4c6b1f3303aee0ea31f0e4ce683abadb7b5d67282

Contents?: true

Size: 1.01 KB

Versions: 6

Compression:

Stored size: 1.01 KB

Contents

module Polyfill
  module V2_3
    module Hash
      def dig(head, *rest)
        [head, *rest].reduce(self) do |value, accessor|
          next_value =
            case value
            when ::Array
              value.at(accessor)
            when ::Hash
              value[accessor]
            when ::Struct
              value[accessor] if value.members.include?(accessor)
            else
              begin
                break value.dig(*rest)
              rescue NoMethodError
                raise TypeError, "#{value.class} does not have a #dig method"
              end
            end

          break nil if next_value.nil?
          next_value
        end
      end

      def fetch_values(*keys)
        keys.each_with_object([]) do |key, values|
          value =
            if block_given?
              fetch(key, &::Proc.new)
            else
              fetch(key)
            end

          values << value
        end
      end

      def to_proc
        method(:[]).to_proc
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
polyfill-1.0.1 lib/polyfill/v2_3/hash.rb
polyfill-1.0.0 lib/polyfill/v2_3/hash.rb
polyfill-0.10.0 lib/polyfill/v2_3/hash.rb
polyfill-0.9.0 lib/polyfill/v2_3/hash.rb
polyfill-0.8.0 lib/polyfill/v2_3/hash.rb
polyfill-0.7.0 lib/polyfill/v2_3/hash.rb