Sha256: 0524d439665ccf89663d0cec650bf11218a200783042714af94092b70810ee01

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require 'mem/version'

module Mem
  def self.included(base)
    base.extend(ClassMethods)
  end

  def has_memoized?(key)
    memoized_table.key?(key)
  end

  def memoize(key, value)
    memoized_table[key] = value
  end

  def memoized(key)
    memoized_table[key]
  end

  def memoized_table
    @memoized_table ||= {}
  end

  def unmemoize(key)
    memoized_table.delete(key)
  end

  module ClassMethods
    def memoize(method_name)
      original_visibility =
        if protected_instance_methods.include?(method_name)
          :protected
        elsif private_instance_methods.include?(method_name)
          :private
        else
          :public
        end

      define_method("#{method_name}_with_memoize") do |*args, &block|
        if has_memoized?(method_name)
          memoized(method_name)
        else
          memoize(method_name, send("#{method_name}_without_memoize", *args, &block))
        end
      end
      send(original_visibility, "#{method_name}_with_memoize")

      alias_method "#{method_name}_without_memoize", method_name
      alias_method method_name, "#{method_name}_with_memoize"

      define_method("unmemoize_#{method_name}") do
        unmemoize(method_name)
      end
      send(original_visibility, "unmemoize_#{method_name}")

      return unless original_visibility != :private

      define_method("#{method_name}=") do |value|
        memoize(method_name, value)
      end
      send(original_visibility, "#{method_name}=")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mem-0.2.0 lib/mem.rb