Sha256: 8628b92ec1c13d506442189fba8eb21d3794fa3294b373178076e4410dbdfbce

Contents?: true

Size: 705 Bytes

Versions: 15

Compression:

Stored size: 705 Bytes

Contents

module Polyfill
  module V2_3
    module Struct
      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
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
polyfill-1.9.0 lib/polyfill/v2_3/struct.rb
polyfill-1.8.0 lib/polyfill/v2_3/struct.rb
polyfill-1.7.0 lib/polyfill/v2_3/struct.rb
polyfill-1.6.0 lib/polyfill/v2_3/struct.rb
polyfill-1.5.0 lib/polyfill/v2_3/struct.rb
polyfill-1.4.0 lib/polyfill/v2_3/struct.rb
polyfill-1.3.0 lib/polyfill/v2_3/struct.rb
polyfill-1.2.0 lib/polyfill/v2_3/struct.rb
polyfill-1.1.0 lib/polyfill/v2_3/struct.rb
polyfill-1.0.1 lib/polyfill/v2_3/struct.rb
polyfill-1.0.0 lib/polyfill/v2_3/struct.rb
polyfill-0.10.0 lib/polyfill/v2_3/struct.rb
polyfill-0.9.0 lib/polyfill/v2_3/struct.rb
polyfill-0.8.0 lib/polyfill/v2_3/struct.rb
polyfill-0.7.0 lib/polyfill/v2_3/struct.rb