Module: LoyalCore::Memoist::InstanceMethods

Defined in:
lib/loyal_core/memoist.rb

Instance Method Summary collapse

Instance Method Details

#flush_cache(*method_names) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/loyal_core/memoist.rb', line 75

def flush_cache(*method_names)
  if method_names.empty?
    prefix = Memoist.unmemoized_prefix+"_"
    method_names = (methods + private_methods + protected_methods).collect do |method_name|
      if method_name.to_s.start_with?(prefix)
        method_name[prefix.length..-1]
      end
    end.compact
  end

  method_names.each do |method_name|
    ivar = Memoist.memoized_ivar_for(method_name)
    instance_variable_get(ivar).clear if instance_variable_defined?(ivar)
  end
end

#memoize_allObject



47
48
49
# File 'lib/loyal_core/memoist.rb', line 47

def memoize_all
  prime_cache
end

#prime_cache(*method_names) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/loyal_core/memoist.rb', line 55

def prime_cache(*method_names)
  if method_names.empty?
    prefix = Memoist.unmemoized_prefix+"_"
    method_names = methods.collect do |method_name|
      if method_name.to_s.start_with?(prefix)
        method_name[prefix.length..-1]
      end
    end.compact
  end

  method_names.each do |method_name|
    if method(Memoist.unmemoized_method_for(method_name)).arity == 0
      __send__(method_name)
    else
      ivar = Memoist.memoized_ivar_for(method_name)
      instance_variable_set(ivar, {})
    end
  end
end

#unmemoize_allObject



51
52
53
# File 'lib/loyal_core/memoist.rb', line 51

def unmemoize_all
  flush_cache
end