Module: Memoist::InstanceMethods

Defined in:
lib/memoist.rb

Instance Method Summary collapse

Instance Method Details

#flush_cache(*method_names) ⇒ Object



95
96
97
98
99
# File 'lib/memoist.rb', line 95

def flush_cache(*method_names)
  memoized_structs(method_names).each do |struct|
    remove_instance_variable(struct.ivar) if instance_variable_defined?(struct.ivar)
  end
end

#memoize_allObject



69
70
71
# File 'lib/memoist.rb', line 69

def memoize_all
  prime_cache
end

#memoized_structs(names) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/memoist.rb', line 77

def memoized_structs(names)
  ref_obj = self.class.respond_to?(:class_eval) ? singleton_class : self
  structs = ref_obj.all_memoized_structs
  return structs if names.empty?

  structs.select { |s| names.include?(s.memoized_method) }
end

#prime_cache(*method_names) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/memoist.rb', line 85

def prime_cache(*method_names)
  memoized_structs(method_names).each do |struct|
    if struct.arity == 0
      __send__(struct.memoized_method)
    else
      instance_variable_set(struct.ivar, {})
    end
  end
end

#unmemoize_allObject



73
74
75
# File 'lib/memoist.rb', line 73

def unmemoize_all
  flush_cache
end