Module: Jets::AwsServices::GlobalMemoist::Macros

Defined in:
lib/jets/aws_services/global_memoist.rb

Instance Method Summary collapse

Instance Method Details

#global_memoize(*methods) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jets/aws_services/global_memoist.rb', line 29

def global_memoize(*methods)
  const_defined?(:"GlobalMemoist#{self.object_id}") ?
    const_get(:"GlobalMemoist#{self.object_id}") : const_set(:"GlobalMemoist#{self.object_id}", Module.new)
  mod = const_get(:"GlobalMemoist#{self.object_id}")

  mod.class_eval do
    methods.each do |method|
      define_method(method) do |skip_cache = false|
        $__memo_methods ||= {}
        if $__memo_methods.include?(method) && !skip_cache
          $__memo_methods[method]
        else
          $__memo_methods[method] = super()
        end
      end
    end
  end
  prepend mod
end

#global_memoize_class_method(*methods) ⇒ Object



49
50
51
52
53
54
# File 'lib/jets/aws_services/global_memoist.rb', line 49

def global_memoize_class_method(*methods)
  singleton_class.class_eval do
    include GlobalMemoist
    global_memoize(*methods)
  end
end