module DbMemoize::Model::ClassMethods

Public Instance Methods

db_memoize(method_name) click to toggle source
# 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_methods() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

create_memoized_alias_method(method_name) click to toggle source

rubocop:disable Style/EmptyBlockParameter

# File lib/db_memoize/model.rb, line 109
def create_memoized_alias_method(method_name)
  unless method_defined?("#{method_name}_without_memoize")
    alias_method "#{method_name}_without_memoize", method_name
  end

  define_method method_name do ||
    memoized_value(method_name)
  end
end
create_memoized_values_association() click to toggle source

rubocop:disable Style/GuardClause

# File lib/db_memoize/model.rb, line 120
def create_memoized_values_association
  unless reflect_on_association(:memoized_values)
    conditions = { entity_table_name: table_name }

    # By defining this before_destroy callback we make sure **we** delete all
    # memoized values before Rails deletes those via `has_many dependent:
    # This leads to has_many later on not finding any values to be deleted.
    #
    # It would be nice if there was a `dependent: :manual/:noop` option.
    #
    # **Note:** before_destroy must be called before memoized_values is
    # set up, to make sure that these things happen in the right order.
    #
    before_destroy do |rec|
      rec.memoized_values.delete_all_ordered
    end

    # memoized_values for this object. These values must be returned
    # newest first, see the comment in \a find_memoized_value.
    has_many :memoized_values, -> { where(conditions).order('created_at DESC') },
             dependent: :delete_all, class_name: 'DbMemoize::Value', foreign_key: :entity_id

  end
end