Sha256: 5546c590abf275d217006a97b2a811e7badbbc34c549011534e2dde52b697dae

Contents?: true

Size: 778 Bytes

Versions: 4

Compression:

Stored size: 778 Bytes

Contents

# frozen_string_literal: true

module Darthjee
  module CoreExt
    module Hash
      class ChainFetcher
        def initialize(hash, *keys, &block)
          @hash = hash
          @keys = keys
          @block = block
        end

        def fetch
          block.present? ? fetch_with_block : fetch_without_block
        end

        private

        attr_reader :hash, :keys, :block

        def fetch_with_block
          @hash = hash.fetch(keys.shift) do |*args|
            missed_keys = keys
            @keys = []
            block.call(*(args + [missed_keys]))
          end until keys.empty?
          hash
        end

        def fetch_without_block
          @hash = hash.fetch(keys.shift) until keys.empty?
          hash
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
darthjee-core_ext-1.7.3 lib/darthjee/core_ext/hash/chain_fetcher.rb
darthjee-core_ext-1.7.2 lib/darthjee/core_ext/hash/chain_fetcher.rb
darthjee-core_ext-1.7.1 lib/darthjee/core_ext/hash/chain_fetcher.rb
darthjee-core_ext-1.7.0 lib/darthjee/core_ext/hash/chain_fetcher.rb