Module: ThreeScale::Backend::Memoizer::Decorator::ClassMethods::Helpers

Defined in:
lib/3scale/backend/memoizer.rb

Class Method Summary collapse

Class Method Details

.get_instance_class(klass) ⇒ Object

helper to go down one level from the current class context ie. the reverse of singleton_class: from metaclass to class



199
200
201
202
203
204
205
206
207
208
# File 'lib/3scale/backend/memoizer.rb', line 199

def get_instance_class(klass)
  return klass unless klass.singleton_class?
  # workaround Ruby's lack of the inverse of singleton_class
  base_s = klass.to_s.split(':').delete_if { |k| k.start_with? '#' }.
    join(':').split('>').first
  iklass = Kernel.const_get(base_s)
  # got the root class, now go up a level shy of self
  iklass = iklass.singleton_class while iklass.singleton_class != klass
  iklass
end

.memoize_class_method(m, partialkey, klass) ⇒ Object



187
188
189
190
191
192
193
194
# File 'lib/3scale/backend/memoizer.rb', line 187

def memoize_class_method(m, partialkey, klass)
  klass.define_singleton_method(m.name) do |*args|
    key = Memoizer.build_args_key partialkey, *args
    Memoizer.memoize_block(key) do
      m.call(*args)
    end
  end
end

.memoize_instance_method(m, klass) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/3scale/backend/memoizer.rb', line 174

def memoize_instance_method(m, klass)
  method_name = m.name
  method_s = method_name.to_s
  klass.send :define_method, method_name do |*args|
    key = Memoizer.build_method_key self.to_s, method_s
    key = Memoizer.build_args_key key, *args
    Memoizer.memoize_block(key) do
      m.bind(self).call(*args)
    end
  end
end