Class: ActiveModelCachers::ActiveRecord::Cacher

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model_cachers/active_record/cacher.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, model: nil) ⇒ Cacher


54
55
56
57
# File 'lib/active_model_cachers/active_record/cacher.rb', line 54

def initialize(id: nil, model: nil)
  @id = id
  @model = model
end

Class Method Details

.define_cacher_method(attr, primary_key, service_klasses) ⇒ Object


8
9
10
11
12
13
14
15
16
# File 'lib/active_model_cachers/active_record/cacher.rb', line 8

def define_cacher_method(attr, primary_key, service_klasses)
  cacher_klass = get_cacher_klass(attr.klass)
  method = attr.column
  return cacher_klass.define_find_by(attr, primary_key, service_klasses) if method == nil
  cacher_klass.attributes << method
  cacher_klass.send(:define_method, method){ exec_by(attr, primary_key, service_klasses, :get) }
  cacher_klass.send(:define_method, "peek_#{method}"){ exec_by(attr, primary_key, service_klasses, :peek) }
  cacher_klass.send(:define_method, "clean_#{method}"){ exec_by(attr, primary_key, service_klasses, :clean_cache) }
end

.define_find_by(attr, primary_key, service_klasses) ⇒ Object


22
23
24
25
26
27
28
29
30
31
# File 'lib/active_model_cachers/active_record/cacher.rb', line 22

def define_find_by(attr, primary_key, service_klasses)
  if @find_by_mapping == nil
    @find_by_mapping = {}
    attributes << :find_by
    define_method(:find_by){|args| exec_find_by(args, :get) }
    define_method(:peek_by){|args| exec_find_by(args, :peek) }
    define_method(:clean_by){|args| exec_find_by(args, :clean_cache) }
  end
  @find_by_mapping[primary_key] = [attr, service_klasses]
end

.get_cacher_klass(klass) ⇒ Object


18
19
20
# File 'lib/active_model_cachers/active_record/cacher.rb', line 18

def get_cacher_klass(klass)
  @defined_map[klass] ||= create_cacher_klass_at(klass)
end

.get_data_from_find_by_mapping(primary_key) ⇒ Object


33
34
35
36
# File 'lib/active_model_cachers/active_record/cacher.rb', line 33

def get_data_from_find_by_mapping(primary_key)
  return if @find_by_mapping == nil
  return @find_by_mapping[primary_key]
end