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

Returns a new instance of Cacher.



65
66
67
68
# File 'lib/active_model_cachers/active_record/cacher.rb', line 65

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

Class Method Details

.define_cacher_method(attr, primary_key, service_klasses) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/active_model_cachers/active_record/cacher.rb', line 12

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.send(:define_methods, method, {
    method            => ->{ exec_by(attr, primary_key, service_klasses, :get) },
    "peek_#{method}"  => ->{ exec_by(attr, primary_key, service_klasses, :peek) },
    "clean_#{method}" => ->{ exec_by(attr, primary_key, service_klasses, :clean_cache) },
  })
end

.define_find_by(attr, primary_key, service_klasses) ⇒ Object



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

def define_find_by(attr, primary_key, service_klasses)
  if @find_by_mapping == nil
    @find_by_mapping = {}
    define_methods(:find_by, {
      :find_by   => ->(args){ exec_find_by(args, :get) },
      :peek_by   => ->(args){ exec_find_by(args, :peek) },
      :clean_by  => ->(args){ exec_find_by(args, :clean_cache) },
    })
  end
  @find_by_mapping[primary_key] = [attr, service_klasses]
end

.get_cacher_klass(klass) ⇒ Object



8
9
10
# File 'lib/active_model_cachers/active_record/cacher.rb', line 8

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