Sha256: aea43a0fb48c972d549dd7b767b0997d7e33dbbdf3febfae36d04ca60fefc080

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

module Polyfill
  module V2_3
    module Array
      module Instance
        module Dig
          module Method
            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

          refine ::Array do
            include Method
          end

          def self.included(base)
            base.include Method
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polyfill-0.6.0 lib/polyfill/v2_3/array/instance/dig.rb