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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jets/aws_services/global_memoist.rb', line 34

def global_memoize(*methods)
  const_defined?(:"GlobalMemoist#{object_id}") ?
    const_get(:"GlobalMemoist#{object_id}") : const_set(:"GlobalMemoist#{object_id}", Module.new)
  mod = const_get(:"GlobalMemoist#{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



54
55
56
57
58
59
# File 'lib/jets/aws_services/global_memoist.rb', line 54

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