Module: RecordCache::ActiveRecordExtension

Defined in:
lib/record_cache.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(mod) ⇒ Object



180
181
182
# File 'lib/record_cache.rb', line 180

def self.extended(mod)
  mod.send(:class_inheritable_accessor, :cached_indexes)
end

Instance Method Details

#record_cache(*args) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/record_cache.rb', line 184

def record_cache(*args)
  extend  RecordCache::ClassMethods
  include RecordCache::InstanceMethods

  opts = args.pop
  opts[:fields] = args
  opts[:class]  = self
  field_lookup  = opts.delete(:field_lookup) || []

  index = RecordCache::Index.new(opts)
  add_cached_index(index)
  first_index = (cached_indexes.size == 1)

  (class << self; self; end).module_eval do
    if index.includes_id?
      [:first, :all, :set, :raw, :ids].each do |type|
        next if type == :ids and index.name == 'by_id'
        define_method( index.find_method_name(type) ) do |keys|
          if self.send(:scope,:find) and self.send(:scope,:find).any?
            self.method_missing(index.find_method_name(type), keys)
          else
            index.find_by_field(keys, self, type)
          end
        end
      end
    end

    if not index.auto_name? and not index.full_record?
      field = index.fields.first if index.fields.size == 1

      define_method( "all_#{index.name.pluralize}_by_#{index.index_field}" ) do |keys|
        index.field_lookup(keys, self, field, :all)
      end
      
      define_method( "#{index.name.pluralize}_by_#{index.index_field}" ) do |keys|
        index.field_lookup(keys, self, field)
      end

      define_method( "#{index.name.singularize}_by_#{index.index_field}" ) do |keys|
        index.field_lookup(keys, self, field, :first)
      end
    end
    
    if index.auto_name?
      (field_lookup + index.fields).each do |field|
        next if field == index.index_field
        plural_field = field.pluralize
        prefix = index.prefix
        prefix = "#{prefix}_" if prefix

        define_method( "all_#{prefix}#{plural_field}_by_#{index.index_field}"  ) do |keys|
          index.field_lookup(keys, self, field, :all)
        end

        define_method( "#{prefix}#{plural_field}_by_#{index.index_field}"  ) do |keys|
          index.field_lookup(keys, self, field)
        end
        
        define_method( "#{prefix}#{field}_by_#{index.index_field}"  ) do |keys|
          index.field_lookup(keys, self, field, :first)
        end
      end
    end
          
    if first_index
      alias_method_chain :find, :caching
      alias_method_chain :update_all, :invalidate
      alias_method_chain :delete_all, :invalidate
    end
  end
  
  if first_index
    after_save     :invalidate_record_cache_deferred
    after_destroy  :invalidate_record_cache_deferred
    after_commit   :complete_deferred_record_cache_invalidations
    after_rollback :complete_deferred_record_cache_invalidations
  end
end