Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cache_it.rb

Class Method Summary collapse

Class Method Details

.cache_it(*index) ⇒ Object

First time called, configures cache_it for this ActiveRecord model. Subsequent calls returns a delegate to access class methods.

Examples:

class Foo < ActiveRecord::Base
  cache_it :first, :last
end

class Foo < ActiveRecord::Base
  cache_it do |c|
    c.index :first, :last
    c.index :email
    c.counters :points
  end
end


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

def self.cache_it(*index)
  if self.class_variable_defined? :@@cache_it
    raise ArgumentError, "cannot reconfigure" if index.present? or block_given?
  else
    raise ArgumentError, "use block or args" if index.present? and block_given?
    config = CacheIt::Config.new self
    config.index *index if config and index.present?
    yield config if config and block_given?
    self.class_exec do
      # Returns delegate to access instance methods
      def cache_it
        @cache_it ||= CacheIt::InstanceDelegate.new self
      end
    end
    delegate = CacheIt::ClassDelegate.new self, config
    self.class_variable_set "@@cache_it", delegate
  end
  self.class_variable_get "@@cache_it"
end