Sha256: eaf123d8a2d932db3ea0c8faf632a65083412998240976ff5ef36b295f0adc38

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module ActiveMemoize
  class Instance
    include ActiveMemoize::Shared

    CALLER_METHOD_REGEX ||= /`([^']*)'/.freeze

    def initialize; end

    def [](key)
      cache[key]
    end

    def []=(key, val)
      cache[key] = val
    end

    def clear
      cache.clear
    end

    def delete(key)
      cache.delete(key)
    end

    # :nocov:
    def each
      cache.each { |key, val| yield(key, val) }
    end
    # :nocov:

    def empty?
      cache.empty?
    end

    alias_method :blank?, :empty?

    def key?(key)
      cache.key?(key)
    end

    alias_method :hit?, :key?

    def keys
      cache.keys
    end

    def merge!(hash)
      cache.merge!(hash)
    end

    def memoize(as: nil, refresh: false, &block)
      key = key(as || caller_method, caller_locals(block))
      return cache[key] if !refresh && cache.key?(key)

      cache[key] = yield(block)
    end

    def present?
      !blank?
    end

    alias_method :hits?, :present?

    def size
      cache.size
    end

    def to_hash
      cache
    end

    alias_method :as_json, :to_hash
    alias_method :hits, :to_hash

    def values
      cache.values
    end

    private

    # rubocop:disable Metrics/LineLength
    def caller_locals(block)
      local_vars = block.binding.local_variables
      local_vars.flat_map { |name| [name, block.binding.local_variable_get(name)] }
    end
    # rubocop:enable Metrics/LineLength

    def caller_method
      val = caller(2..2).first[CALLER_METHOD_REGEX, 1]
      return val unless val.include?('<top (required)>')

      caller(1..1).first[CALLER_METHOD_REGEX, 1]
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_memoize-2.0.2 lib/active_memoize/instance.rb
active_memoize-2.0.1 lib/active_memoize/instance.rb
active_memoize-2.0.0 lib/active_memoize/instance.rb