merged cont.
[opensuse:yast-rest-service.git] / webyast / vendor / plugins / static_record_cache / lib / static_active_record_context.rb
1 # ==StaticActiveRecordContext
2 # Simple module to extends technoweenie's rad ActiveRecordContext plugin
3 # http://svn.techno-weenie.net/projects/plugins/active_record_context/
4 # to permanently cache active record data for the life of the class
5 #
6 # As with active_record_context, only finds based on ids are cache hits, however
7 # id finders are the majority of calls from associations. If cache hits on
8 # fields and methods are needed, refer to acts_as_static_record[link:files/acts_as_static_record_rb.html]
9 #
10 #  class TelephoneCarriers < ActiveRecord::Base
11 #    extend StaticActiveRecordContext
12 #    has_many :phone_numbers
13 #  end
14 #  
15 # The following would exercise a cache hit
16 #  phone_number.telephone_carrier
17 #
18 #
19 # The static cache is available both inside and outside +with_context+ block, where as
20 # the cache for typical records the context is only with the block.
21 #
22 #  PhoneNumber.with_context {
23 #    PhoneNumber.find :all
24 #    TelephoneCarriers.find :all
25 #  }
26 #
27 #  phone = PhoneNumber.find_by_id(1)             # not a cache hit
28 #  phone.telephone_carrier                       # cache hit
29 #  telephone_carrier = TelephoneCarrier.find(1)  # cache hit
30 #
31 # === Developers
32 # * Blythe Dunham http://snowgiraffe.com
33 #
34 # === Homepage
35 # * Rdoc: http://snowgiraffe.com/rdocs/static_record_cache/
36 # * Github Project: http://github.com/blythedunham/static_record_cache/tree/master
37 # * Install:  <tt>script/plugin install git://github.com/blythedunham/static_record_cache.git</tt>
38 module StaticActiveRecordContext
39   def self.extended(base)#:nodoc
40     base.class_inheritable_accessor :static_record_context
41   end
42
43   def context_cache#:nodoc:
44     self.static_record_context ||= {}
45   end
46
47   def context_cache=(map)#:nodoc:
48     self.static_record_context = map unless map.nil?
49   end
50
51   # Reload the cache for this class only
52   def reload_context_cache
53     self.static_record_context = {}
54   end
55
56   # Call ActiveRecord::Base to cache the other objects
57   def with_context(&block)#:nodoc:
58     ActiveRecord::Base.with_context(&block)
59   end
60 end