Module: ActiveCMIS::Internal::Caching::ClassMethods

Defined in:
lib/active_cmis/internal/caching.rb

Overview

A module for internal use only.

Instance Method Summary collapse

Instance Method Details

#cache(*names) ⇒ void

This method returns an undefined value.

Creates a proxy method for the given method names that caches the result.

Parameters are passed and ignored, cached values will be returned regardless of the parameters.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_cmis/internal/caching.rb', line 16

def cache(*names)
  (@cached_methods ||= []).concat(names).uniq!
  names.each do |name|
    alias_method("#{name}__uncached", name)
    class_eval "if private_method_defined? :\"\#{name}\"\nprivate_method = true\nend\ndef \#{name}(*a, &b)\nif defined? @\#{name}\[email protected]\#{name}\nelse\[email protected]\#{name} = \#{name}__uncached(*a, &b)\nend\nend\nif private_method\nprivate :\"\#{name}__uncached\"\nprivate :\"\#{name}\"\nend\n", __FILE__, __LINE__+1
  end
  reloadable
end

#cached_reader(*names) ⇒ void

This method returns an undefined value.

Creates methods to retrieve attributes with the given names.

If the given attribute does not yet exist the method #load_from_data will be called


46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/active_cmis/internal/caching.rb', line 46

def cached_reader(*names)
  (@cached_methods ||= []).concat(names).uniq!
  names.each do |name|
    define_method "#{name}" do
      if instance_variable_defined? "@#{name}"
        instance_variable_get("@#{name}")
      else
        load_from_data # FIXME: make flexible?
        instance_variable_get("@#{name}")
      end
    end
  end
  reloadable
end