Module: DbMemoize::Model::ClassMethods

Defined in:
lib/db_memoize/model.rb

Instance Method Summary collapse

Instance Method Details

#db_memoize(method_name) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/db_memoize/model.rb', line 66

def db_memoize(method_name)
  @db_memoized_methods ||= []
  @db_memoized_methods.push(method_name.to_sym)

  # [TODO] - should the create_memoized_** functions really be called
  # when the method_name is in @db_memoized_methods already?
  create_memoized_alias_method(method_name)
  create_memoized_values_association
end

#db_memoized_methodsObject



76
77
78
79
# File 'lib/db_memoize/model.rb', line 76

def db_memoized_methods
  methods = @db_memoized_methods || []
  superclass.respond_to?(:db_memoized_methods) ? (superclass.db_memoized_methods + methods).uniq : methods
end

#memoize_values(records_or_ids, values) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/db_memoize/model.rb', line 91

def memoize_values(records_or_ids, values)
  transaction do
    ids = Helpers.find_ids(records_or_ids)

    ids.each do |id|
      values.each do |method_name, value|
        ::DbMemoize::Value.fast_create table_name, id, method_name, value
      end
    end
  end
end

#unmemoize(records_or_ids, method_name = :all) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/db_memoize/model.rb', line 81

def unmemoize(records_or_ids, method_name = :all)
  conditions = {
    entity_table_name: table_name,
    entity_id: Helpers.find_ids(records_or_ids)
  }
  conditions[:method_name] = method_name unless method_name == :all

  DbMemoize::Value.where(conditions).delete_all_ordered
end