public
Description: Allows the caching of lookup data in your Rails models
Clone URL: git://github.com/vigetlabs/constant_cache.git
Start of refactoring to handle AR lifecycle hooks

git-svn-id: 
http://svn.extendviget.com/lab/trunk/plugins/caches_constants@452 
e959a6d6-1924-0410-92b3-a6fa492f4c66
reagent (author)
Wed Nov 21 11:16:38 -0800 2007
commit  1321953a082e0da3707e4bad604e2c3bd2a0307d
tree    8e4667b6c72d811146643b597a6eb74af72be93f
parent  350806f8a30a53d0289c9cd3234f5e26d350dca4
...
5
6
7
 
...
5
6
7
8
0
@@ -5,3 +5,4 @@ require "#{lib_dir}/constant_cache"
0
 
0
 String.send(:include, Viget::Format)
0
 ActiveRecord::Base.send(:extend, Viget::ConstantCache::ClassMethods)
0
+ActiveRecord::Base.send(:include, Viget::ConstantCache::InstanceMethods)
...
3
4
5
6
7
8
9
10
11
12
13
 
 
 
 
 
 
 
 
 
 
 
 
 
14
15
16
 
17
18
19
20
...
3
4
5
 
 
 
 
 
 
 
 
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
0
@@ -3,17 +3,23 @@ module Viget
0
     module ClassMethods
0
       def caches_constants(additional_options = {})
0
         options = {:key => :name, :limit => 64}.merge(additional_options)
0
- find(:all).each do |model|
0
- const = model.send(options[:key].to_sym).constant_name
0
- unless const.blank?
0
- const = const[0, options[:limit]]
0
- if !const.blank?
0
- raise RuntimeError, "Constant #{self.to_s}::#{const} has already been defined" if const_defined?(const)
0
- const_set(const, model) if !const.blank?
0
- end
0
+ find(:all).each {|model| model.set_instance_as_constant(options) }
0
+ end
0
+ end
0
+
0
+ module InstanceMethods
0
+
0
+ def set_instance_as_constant(options)
0
+ const = self.send(options[:key].to_sym).constant_name
0
+ unless const.blank?
0
+ const = const[0, options[:limit]]
0
+ if !const.blank?
0
+ raise RuntimeError, "Constant #{self.class.to_s}::#{const} has already been defined" if self.class.const_defined?(const)
0
+ self.class.const_set(const, self) if !const.blank?
0
           end
0
         end
0
       end
0
+
0
     end
0
   end
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.