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.

Parameters:

  • Names (Symbol, <Symbol>)

    of methods that will be cached



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 <<-RUBY, __FILE__, __LINE__+1
      if private_method_defined? :"#{name}"
        private_method = true
      end
      def #{name}(*a, &b)
        if defined? @#{name}
          @#{name}
        else
          @#{name} = #{name}__uncached(*a, &b)
        end
      end
      if private_method
        private :"#{name}__uncached"
        private :"#{name}"
      end
    RUBY
  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

Parameters:

  • Names (Symbol, <Symbol>)

    of desired attributes



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