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
Search Repo:
refactored the pieces apart

git-svn-id: http://svn.6brand.com/projects/plugins/cached_values@354 
7491b73d-821b-0410-9297-ad1f6b5b4194
studioda (author)
Fri Aug 31 08:58:19 -0700 2007
commit  36fe5e1742b2e2e5b1bcb4e1445ba178b82d539b
tree    8f9f51588cdb206cb5ecde592c53085711add4dd
parent  72148ad1d259541dfb68e03fe479669c15fa91c0
...
 
1
2
 
...
1
2
 
3
0
@@ -1,2 +1,3 @@
0
+require 'cached_value'
0
 require 'cached_values'
0
-ActiveRecord::Base.send :include, HasCachedValueExtension
0
+ActiveRecord::Base.send :include, CachedValues
...
39
40
41
42
 
43
44
45
...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
...
39
40
41
 
42
43
44
45
...
80
81
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -39,7 +39,7 @@ module CachedValues # :nodoc:
0
 
0
       configure_dependency_for_cached_value(reflection)
0
 
0
- reflection.options[:cache] ||= reflection.name unless false == reflection.options[:counter_cache]
0
+ reflection.options[:cache] ||= reflection.name unless false == options[:cache]
0
 
0
       cached_value_accessor_method(reflection, ActiveRecord::CachedValue)
0
     end
0
@@ -80,118 +80,3 @@ module CachedValues # :nodoc:
0
       end
0
   end
0
 end
0
-
0
-module ActiveRecord
0
- class CachedValue
0
- attr_reader :reflection
0
- instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$)/ }
0
-
0
- def initialize(owner, reflection)
0
- @owner, @reflection = owner, reflection
0
- reset
0
- end
0
-
0
- def reset
0
- @target = nil
0
- @loaded = false
0
- end
0
-
0
- def load
0
- reset
0
- load_target
0
- end
0
-
0
- def reload
0
- reset
0
- clear_cache
0
- load_target
0
- end
0
-
0
- def clear
0
- clear_cache
0
- @owner.instance_variable_set("@#{@reflection.name}", nil)
0
- end
0
-
0
- def loaded?
0
- @loaded
0
- end
0
-
0
- def loaded
0
- @loaded = true
0
- end
0
-
0
- def target
0
- @target
0
- end
0
-
0
- protected
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 find_target
0
- target = find_target_from_cache
0
- unless target
0
- target ||= @reflection.options[:sql] ? find_target_by_sql : find_target_by_eval
0
- update_cache(target)
0
- end
0
- target
0
- end
0
-
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
- 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 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)
0
- @owner.send(:interpolate_sql, sql, record)
0
- end
0
-
0
- def sanitize_sql(sql)
0
- @owner.class.send(:sanitize_sql, sql)
0
- end
0
-
0
- def method_missing(method, *args, &block)
0
- if load_target
0
- @target.send(method, *args, &block)
0
- end
0
- end
0
- end
0
-end

Comments

    No one has commented yet.