Module: SimpleMemoize::Module

Defined in:
lib/simple_memoize.rb

Instance Method Summary collapse

Instance Method Details

#memoize(*method_names) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/simple_memoize.rb', line 5

def memoize(*method_names)
  method_names.each do |method_name|
    method_name = method_name.to_s
    stripped_method_name = method_name.sub(/([!?])$/, '')

    punctuation = $1
    wordy_punctuation = (punctuation == '!' ? '_bang' : '_huh') if punctuation
    ivar_name = "@#{stripped_method_name}#{wordy_punctuation}"

    memoized_method_name = "#{stripped_method_name}_with_memo#{punctuation}"
    regular_method_name  = "#{stripped_method_name}_without_memo#{punctuation}"

    unless __include_method_name__( (instance_methods + private_instance_methods), method_name )
      raise NoMethodError, "The Method '#{method_name}' cannot be memoized because it doesn't exist in #{self}"
    end
    return if self.method_defined?(memoized_method_name)

    self.class_eval "
      def #{memoized_method_name}(*args)
        if defined?(#{ivar_name})
          #{ivar_name}
        else
          #{ivar_name} = #{regular_method_name}(*args)
        end
      end

      alias_method :#{regular_method_name}, :#{method_name}
      alias_method :#{method_name}, :#{memoized_method_name}

      protected :#{method_name} if __include_method_name__( protected_instance_methods, '#{regular_method_name}' )
      private   :#{method_name} if __include_method_name__( private_instance_methods, '#{regular_method_name}' )
    "
  end
end