Module: Lemo::MemoedMethods

Included in:
Memo, Ormo
Defined in:
lib/lemo/memoed_methods.rb

Instance Method Summary collapse

Instance Method Details

#_clear_memos(*requested_meths) ⇒ Object

Reset some or all memoized variables. Return cleared value(s) Has to do quite a lot of meta-work, so don’t put this in fast-path code.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lemo/memoed_methods.rb', line 23

def _clear_memos( *requested_meths )
  # construct set of memoed methods to clear
  requested_meths =
  if requested_meths.empty?
    _memoed_methods.keys
  else
    # only clear ivars that actually make sense
    _memoed_methods.keys & requested_meths
  end

  # clear set of memos and keep their values
  memoed_values = requested_meths.map do |meth|
    if instance_variable_defined?( ivar = _memoed_methods[meth].owner.ivar_from(meth) )
      remove_instance_variable(ivar)
    end
  end

  # return nil, the first value, or all values
  (0..1) === memoed_values.size ? memoed_values.first : memoed_values
end

#_memoed_methodsObject

the set of methods memoed so far that we know about.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/lemo/memoed_methods.rb', line 6

def _memoed_methods
  methods = {}

  if self.class.respond_to?(:memoed_methods)
    methods.merge! self.class.memoed_methods
  end

  if singleton_methods.size > 0 && singleton_class.respond_to?(:memoed_methods)
    methods.merge! singleton_class.memoed_methods
  end

  methods
end