Sha256: 2eb6c977f98855e17ad709b741b44fd8cbbd16c34b750407a093cbf38c838fbc

Contents?: true

Size: 673 Bytes

Versions: 1

Compression:

Stored size: 673 Bytes

Contents

# frozen_string_literal: true

module Kind
  module Dig
    extend self

    def call(data, keys)
      return unless keys.is_a?(Array)

      keys.reduce(data) do |memo, key|
        value = get(memo, key)

        break if value.nil?

        value
      end
    end

    private

      def get(data, key)
        return data[key] if Hash === data

        case data
        when Array
          data[key] if key.respond_to?(:to_int)
        when OpenStruct
          data[key] if key.respond_to?(:to_sym)
        when Struct
          if key.respond_to?(:to_int) || key.respond_to?(:to_sym)
            data[key] rescue nil
          end
        end
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kind-3.1.0 lib/kind/dig.rb