Sha256: 299dc1dac948b67a883598d865403eda081952a03a5bc7df56190fca50df2adb

Contents?: true

Size: 630 Bytes

Versions: 3

Compression:

Stored size: 630 Bytes

Contents

# frozen_string_literal: true

class 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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
darthjee-core_ext-1.6.2 lib/darthjee/core_ext/hash/chain_fetcher.rb
darthjee-core_ext-1.6.1 lib/darthjee/core_ext/hash/chain_fetcher.rb
darthjee-core_ext-1.6.0 lib/darthjee/core_ext/hash/chain_fetcher.rb