Module: DbMemoize::Model::ClassMethods

Defined in:
lib/db_memoize/model.rb

Instance Method Summary collapse

Instance Method Details

#db_memoize(method_name) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/db_memoize/model.rb', line 69

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



79
80
81
82
# File 'lib/db_memoize/model.rb', line 79

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



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/db_memoize/model.rb', line 94

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



84
85
86
87
88
89
90
91
92
# File 'lib/db_memoize/model.rb', line 84

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