Module: Memoist

Defined in:
lib/memoist.rb,
lib/memoist/version.rb

Defined Under Namespace

Modules: InstanceMethods Classes: MemoizedMethod

Constant Summary collapse

VERSION =
'0.16.2'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.escape_punctuation(string) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/memoist.rb', line 40

def self.escape_punctuation(string)
  string = string.is_a?(String) ? string.dup : string.to_s.dup

  return string unless string.end_with?('?'.freeze, '!'.freeze)

  # A String can't end in both ? and !
  if string.sub!(/\?\Z/, '_query'.freeze)
  else
    string.sub!(/!\Z/, '_bang'.freeze)
  end
  string
end

.extended(extender) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/memoist.rb', line 6

def self.extended(extender)
  Memoist.memoist_eval(extender) do
    unless singleton_class.method_defined?(:memoized_methods)
      def self.memoized_methods
        @_memoized_methods ||= []
      end
    end
  end
end

.extract_reload!(method, args) ⇒ Object



61
62
63
64
65
66
# File 'lib/memoist.rb', line 61

def self.extract_reload!(method, args)
  if args.length == method.arity.abs + 1 && (args.last == true || args.last == :reload)
    reload = args.pop
  end
  reload
end

.memoist_eval(klass, *args, &block) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/memoist.rb', line 53

def self.memoist_eval(klass, *args, &block)
  if klass.respond_to?(:class_eval)
    klass.class_eval(*args, &block)
  else
    klass.singleton_class.class_eval(*args, &block)
  end
end

.memoized_ivar_for(method_name, identifier = nil) ⇒ Object



16
17
18
# File 'lib/memoist.rb', line 16

def self.memoized_ivar_for(method_name, identifier = nil)
  "@#{memoized_prefix(identifier)}_#{escape_punctuation(method_name)}"
end

.memoized_prefix(identifier = nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/memoist.rb', line 24

def self.memoized_prefix(identifier = nil)
  if identifier
    "_memoized_#{identifier}"
  else
    '_memoized'.freeze
  end
end

.unmemoized_method_for(method_name, identifier = nil) ⇒ Object



20
21
22
# File 'lib/memoist.rb', line 20

def self.unmemoized_method_for(method_name, identifier = nil)
  "#{unmemoized_prefix(identifier)}_#{method_name}".to_sym
end

.unmemoized_prefix(identifier = nil) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/memoist.rb', line 32

def self.unmemoized_prefix(identifier = nil)
  if identifier
    "_unmemoized_#{identifier}"
  else
    '_unmemoized'.freeze
  end
end

Instance Method Details

#all_memoized_structsObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/memoist.rb', line 104

def all_memoized_structs
  @all_memoized_structs ||= begin
    structs = memoized_methods.dup

    # Collect the memoized_methods of ancestors in ancestor order
    # unless we already have it since self or parents could be overriding
    # an ancestor method.
    ancestors.grep(Memoist).each do |ancestor|
      ancestor.memoized_methods.each do |m|
        structs << m unless structs.any? { |am| am.memoized_method == m.memoized_method }
      end
    end
    structs
  end
end

#clear_structsObject



120
121
122
# File 'lib/memoist.rb', line 120

def clear_structs
  @all_memoized_structs = nil
end

#memoize(*method_names) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/memoist.rb', line 124

def memoize(*method_names)
  identifier = method_names.pop[:identifier] if method_names.last.is_a?(Hash)

  method_names.each do |method_name|
    unmemoized_method = Memoist.unmemoized_method_for(method_name, identifier)
    memoized_ivar = Memoist.memoized_ivar_for(method_name, identifier)

    Memoist.memoist_eval(self) do
      include InstanceMethods

      if method_defined?(unmemoized_method)
        warn "Already memoized #{method_name}"
        return
      end
      alias_method unmemoized_method, method_name

      mm = MemoizedMethod.new(method_name, memoized_ivar, instance_method(method_name).arity)
      memoized_methods << mm
      if mm.arity == 0

        # define a method like this;

        # def mime_type(reload=true)
        #   skip_cache = reload || !instance_variable_defined?("@_memoized_mime_type")
        #   set_cache = skip_cache && !frozen?
        #
        #   if skip_cache
        #     value = _unmemoized_mime_type
        #   else
        #     value = @_memoized_mime_type
        #   end
        #
        #   if set_cache
        #     @_memoized_mime_type = value
        #   end
        #
        #   value
        # end

        module_eval <<-EOS, __FILE__, __LINE__ + 1
          def #{method_name}(reload = false)
            skip_cache = reload || !instance_variable_defined?("#{memoized_ivar}")
            set_cache = skip_cache && !frozen?

            if skip_cache
              value = #{unmemoized_method}
            else
              value = #{memoized_ivar}
            end

            if set_cache
              #{memoized_ivar} = value
            end

            value
          end
        EOS
      else

        # define a method like this;

        # def mime_type(*args)
        #   reload = Memoist.extract_reload!(method(:_unmemoized_mime_type), args)
        #
        #   skip_cache = reload || !memoized_with_args?(:mime_type, args)
        #   set_cache = skip_cache && !frozen
        #
        #   if skip_cache
        #     value = _unmemoized_mime_type(*args)
        #   else
        #     value = @_memoized_mime_type[args]
        #   end
        #
        #   if set_cache
        #     @_memoized_mime_type ||= {}
        #     @_memoized_mime_type[args] = value
        #   end
        #
        #   value
        # end

        module_eval <<-EOS, __FILE__, __LINE__ + 1
          def #{method_name}(*args)
            reload = Memoist.extract_reload!(method(#{unmemoized_method.inspect}), args)

            skip_cache = reload || !(instance_variable_defined?(#{memoized_ivar.inspect}) && #{memoized_ivar} && #{memoized_ivar}.has_key?(args))
            set_cache = skip_cache && !frozen?

            if skip_cache
              value = #{unmemoized_method}(*args)
            else
              value = #{memoized_ivar}[args]
            end

            if set_cache
              #{memoized_ivar} ||= {}
              #{memoized_ivar}[args] = value
            end

            value
          end
        EOS
      end

      if private_method_defined?(unmemoized_method)
        private method_name
      elsif protected_method_defined?(unmemoized_method)
        protected method_name
      end
    end
  end
  # return a chainable method_name symbol if we can
  method_names.length == 1 ? method_names.first : method_names
end