Module: DbMemoize::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/db_memoize/model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#memoize_values(values) ⇒ Object

Used to set multiple memoized values in one go.

Example:

product.memoize_values full_title: "my full title",
                       autocomplete_info: "my autocomplete_info"

This sets the “full_title” and “autocomplete_info” values of the product.



36
37
38
39
40
# File 'lib/db_memoize/model.rb', line 36

def memoize_values(values)
  values.each do |name, value|
    create_memoized_value(name, value)
  end
end

#memoized_value(method_name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/db_memoize/model.rb', line 7

def memoized_value(method_name)
  memoizable = !changed? && persisted?
  return send("#{method_name}_without_memoize") unless memoizable

  memoized_value = find_memoized_value(method_name)

  if memoized_value
    memoized_value.value
  else
    value = send("#{method_name}_without_memoize")
    create_memoized_value(method_name, value)
    value
  end
end

#unmemoize(method_name = :all) ⇒ Object



22
23
24
# File 'lib/db_memoize/model.rb', line 22

def unmemoize(method_name = :all)
  self.class.unmemoize id, method_name
end