Module: LoyalCore::Memoist

Included in:
ActsAsContentModeAble::ClassMethods::ContentModeInstanceMethods
Defined in:
lib/loyal_core/memoist.rb

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.escape_punctuation(string) ⇒ Object



27
28
29
# File 'lib/loyal_core/memoist.rb', line 27

def self.escape_punctuation(string)
  string.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')
end

.extract_reload!(method, args) ⇒ Object



39
40
41
42
43
44
# File 'lib/loyal_core/memoist.rb', line 39

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

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



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

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



11
12
13
# File 'lib/loyal_core/memoist.rb', line 11

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

.memoized_prefix(identifier = nil) ⇒ Object



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

def self.memoized_prefix(identifier=nil)
  ["_memoized", identifier].compact.join("_")
end

.unmemoized_method_for(method_name, identifier = nil) ⇒ Object



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

def self.unmemoized_method_for(method_name, identifier=nil)
  [unmemoized_prefix(identifier), method_name].join("_").to_sym
end

.unmemoized_prefix(identifier = nil) ⇒ Object



23
24
25
# File 'lib/loyal_core/memoist.rb', line 23

def self.unmemoized_prefix(identifier=nil)
  ["_unmemoized", identifier].compact.join("_")
end

Instance Method Details

#memoize(*method_names) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
# File 'lib/loyal_core/memoist.rb', line 92

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

  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)
        raise "Already memoized #{method_name}"
      end
      alias_method unmemoized_method, method_name

      if instance_method(method_name).arity == 0

        # define a method like this;

        # def mime_type(reload=true)
        #   skip_cache = reload || !memoized?(:abc)
        #   set_cache = skip_cache && !frozen?
        #
        #   if skip_cache
        #     value = _unmemoized_mime_type
        #   else
        #     value = @_memoized_mime_type[0]
        #   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 || !defined?(#{memoized_ivar}) || #{memoized_ivar}.empty?
          set_cache = skip_cache && !frozen?

          if skip_cache
            value = #{unmemoized_method}
          else
            value = #{memoized_ivar}[0]
          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 || !(defined?(#{memoized_ivar}) && #{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
end