public
Rubygem
Description: Rails Plugin - memoize and store to the db a record attribute generated by any expression or SQL query.
Homepage: http://6brand.com
Clone URL: git://github.com/JackDanger/cached_values.git
rearranged the private methods and removed references to counter_cache

git-svn-id: http://svn.6brand.com/projects/plugins/cached_values@351 
7491b73d-821b-0410-9297-ad1f6b5b4194
studioda (author)
Fri Aug 31 08:28:27 -0700 2007
commit  2fb369420bf512dcb8266b79dd01ac1b5d3c3e45
tree    e01b1c965509009da17f0992f48c1cb11cf084b1
parent  d867d0fe9661dd5af9459899a35afc49d6e2d22b
...
34
35
36
37
38
 
 
39
40
41
42
43
 
44
45
 
46
47
48
49
50
51
52
53
54
55
56
...
61
62
63
64
65
 
 
66
67
68
 
 
69
70
71
72
 
73
74
75
...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
 
 
 
 
 
149
150
151
...
157
158
159
160
161
162
163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
165
 
 
 
 
 
 
 
 
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
 
 
 
 
 
 
181
182
183
...
34
35
36
 
 
37
38
39
40
41
 
 
42
43
 
44
45
46
47
48
49
 
 
 
50
51
52
...
57
58
59
 
 
60
61
62
 
 
63
64
65
66
67
 
68
69
70
71
...
125
126
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
 
129
130
131
132
133
134
135
136
...
142
143
144
 
 
 
 
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
 
161
162
163
164
165
166
167
168
169
170
171
172
173
174
 
 
 
 
 
 
 
 
 
175
176
177
178
179
180
181
182
183
0
@@ -34,23 +34,19 @@ module CachedValues # :nodoc:
0
     # end
0
     #
0
     
0
- def caches_value(association_id, options = {})
0
- reflection = create_cached_value_reflection(association_id, options)
0
+ def caches_value(name, options = {})
0
+ reflection = create_cached_value_reflection(name, options)
0
 
0
       configure_dependency_for_cached_value(reflection)
0
 
0
- reflection.options[:counter_cache] = reflection.options.delete(:cache) if reflection.options[:cache]
0
- reflection.options[:counter_cache] ||= reflection.name unless false == reflection.options[:counter_cache]
0
+ reflection.options[:cache] ||= reflection.name unless false == reflection.options[:counter_cache]
0
 
0
- association_accessor_method(reflection, ActiveRecord::CachedValue)
0
+ cached_value_accessor_method(reflection, ActiveRecord::CachedValue)
0
     end
0
 
0
     private
0
     
0
       def configure_dependency_for_cached_value(reflection)
0
- if reflection.options[:counter_cache] && reflection.options[:cache]
0
- raise ArgumentError, ":cache is an alias for :counter_cache, don't use both options for caches_value in #{self.name}"
0
- end
0
       
0
         if !reflection.options[:sql] && !reflection.options[:eval]
0
           raise ArgumentError, "You must specify either the :eval or :sql options for caches_value in #{self.name}"
0
@@ -61,15 +57,15 @@ module CachedValues # :nodoc:
0
         end
0
       end
0
     
0
- def create_cached_value_reflection(association_id, options)
0
- options.assert_valid_keys(:sql, :eval, :cache, :counter_cache)
0
+ def create_cached_value_reflection(name, options)
0
+ options.assert_valid_keys(:sql, :eval, :cache)
0
         
0
- reflection = ActiveRecord::Reflection::MacroReflection.new(:cached_value, association_id, options, self)
0
- write_inheritable_hash :reflections, association_id => reflection
0
+ reflection = ActiveRecord::Reflection::MacroReflection.new(:cached_value, name, options, self)
0
+ write_inheritable_hash :reflections, name => reflection
0
         reflection
0
       end
0
 
0
- def association_accessor_method(reflection, association_proxy_class)
0
+ def cached_value_accessor_method(reflection, association_proxy_class)
0
         define_method(reflection.name) do |*params|
0
           force_reload = params.first unless params.empty?
0
           association = instance_variable_get("@#{reflection.name}")
0
@@ -129,23 +125,12 @@ module ActiveRecord
0
     end
0
 
0
     protected
0
-
0
- def find_target_by_eval
0
- if @reflection.options[:eval].is_a?(String)
0
- eval(@reflection.options[:eval], @owner.send(:binding))
0
- elsif @reflection.options[:eval].is_a?(Proc)
0
- @reflection.options[:eval].call(@owner)
0
- else
0
- raise ArgumentError.new("The :eval option on a cached_values must be either a String or a Proc")
0
- end
0
- end
0
-
0
- def find_target_by_sql
0
- @owner.class.count_by_sql(sanitize_sql(interpolate_sql(@reflection.options[:sql])))
0
- end
0
       
0
- def find_target_from_cache
0
- @owner.send(:read_attribute, @reflection.counter_cache_column) if has_cached_counter?
0
+ def load_target
0
+ return nil unless defined?(@loaded)
0
+ @target = find_target unless loaded?
0
+ @loaded = true
0
+ @target
0
       end
0
 
0
       def find_target
0
@@ -157,27 +142,42 @@ module ActiveRecord
0
         target
0
       end
0
       
0
- def update_cache(value)
0
- return unless has_cached_counter?
0
- unless @owner.new_record?
0
- @owner.class.update_all(["#{@reflection.counter_cache_column} = ?", value], ["id = ?", @owner.id])
0
+ def find_target_from_cache
0
+ @owner.send(:read_attribute, cache_column) if has_cached_counter?
0
+ end
0
+
0
+ def find_target_by_sql
0
+ @owner.class.count_by_sql(sanitize_sql(interpolate_sql(@reflection.options[:sql])))
0
+ end
0
+
0
+ def find_target_by_eval
0
+ if @reflection.options[:eval].is_a?(String)
0
+ eval(@reflection.options[:eval], @owner.send(:binding))
0
+ elsif @reflection.options[:eval].is_a?(Proc)
0
+ @reflection.options[:eval].call(@owner)
0
+ else
0
+ raise ArgumentError.new("The :eval option on a cached_values must be either a String or a Proc")
0
         end
0
- @owner.send(:write_attribute, @reflection.counter_cache_column, value)
0
+ end
0
+
0
+ def cache_column
0
+ @reflection.options[:cache]
0
+ end
0
+
0
+ def has_cache?
0
+ @reflection.options[:cache] && @owner.attribute_names.include?(@reflection.options[:cache].to_s)
0
       end
0
       
0
       def clear_cache
0
         update_cache(nil)
0
       end
0
       
0
- def load_target
0
- return nil unless defined?(@loaded)
0
- @target = find_target unless loaded?
0
- @loaded = true
0
- @target
0
- end
0
-
0
- def has_cached_counter?
0
- @reflection.options[:counter_cache] && @owner.attribute_names.include?(@reflection.options[:counter_cache].to_s)
0
+ def update_cache(value)
0
+ return unless has_cache?
0
+ unless @owner.new_record?
0
+ @owner.class.update_all(["#{cache_column} = ?", value], ["id = ?", @owner.id])
0
+ end
0
+ @owner.send(:write_attribute, cache_column, value)
0
       end
0
       
0
       def interpolate_sql(sql, record = nil)

Comments

    No one has commented yet.