Module: RuboCop::Isucon::MemorizeMethods

Defined in:
lib/rubocop/isucon/memorize_methods.rb

Overview

Memorize helper

Instance Method Summary collapse

Instance Method Details

#memorize(method_name) ⇒ Object

Define memorize method

Examples:

Usage

extends RuboCop::Isucon::MemorizeMethods

memorize :foo

Generated followings

def foo_with_cache
  @foo_with_cache ||= foo_without_cache
end
alias_method :foo_without_cache, :foo
alias_method :foo, :foo_with_cache

Parameters:

  • method_name (String, Symbol)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubocop/isucon/memorize_methods.rb', line 23

def memorize(method_name)
  class_eval <<~RUBY, __FILE__, __LINE__ + 1
    # def foo_with_cache
    #   @foo_with_cache ||= foo_without_cache
    # end
    def #{method_name}_with_cache
      @#{method_name}_with_cache ||= #{method_name}_without_cache
    end
  RUBY

  alias_method :"#{method_name}_without_cache", method_name
  alias_method method_name, :"#{method_name}_with_cache"
end