Created
March 1, 2009 07:10
-
-
Save Mikr0Tik/72250 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Acts | |
module Cacheable | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def acts_as_cacheable(key, options = {}) | |
extend Acts::Cacheable::SingletonMethods | |
find(:all, options).each{|instance| cached_objects[instance.send(key).to_s] = instance} | |
end | |
end | |
module SingletonMethods | |
def [] (key) | |
cached_objects[key] | |
end | |
def cached(id) | |
cached_objects.each_value{|instance| return instance if instance.id == id} | |
end | |
def objects | |
cached_objects.values | |
end | |
private | |
def cached_objects | |
@all_cached_objects ||= Hash.new | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person < ActiveRecord::Base | |
acts_as_cacheable :login, :conditions => {:admin => true} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Unit < ActiveRecord::Base | |
acts_as_cacheable :key | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment