Class: Cuprum::Rails::Actions::Middleware::Associations::Cache

Inherits:
Command
  • Object
show all
Defined in:
lib/cuprum/rails/actions/middleware/associations/cache.rb

Overview

Pre-warm the association cache for a resource.

Constant Summary collapse

ACTIVE_RECORD_STRATEGY =

Strategy for caching an association value on an ActiveRecord model.

lambda do |entity:, name:, value:|
  entity.send(:association_instance_set, name, value)

  entity
end
DEFAULT_STRATEGY =

Generic strategy for caching an association value.

lambda do |entity:, name:, value:|
  entity[name] = value

  entity
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association:, resource:) ⇒ Cache

Returns a new instance of Cache.

Parameters:

  • association (Cuprum::Associations::Association)

    the association to cache.

  • resource (Cuprum::Rails::Resource)

    the resource to cache.



53
54
55
56
57
58
# File 'lib/cuprum/rails/actions/middleware/associations/cache.rb', line 53

def initialize(association:, resource:)
  super()

  @association = association
  @resource    = resource
end

Instance Attribute Details

#associationCuprum::Associations::Association (readonly)

Returns the association to cache.

Returns:

  • (Cuprum::Associations::Association)

    the association to cache.



61
62
63
# File 'lib/cuprum/rails/actions/middleware/associations/cache.rb', line 61

def association
  @association
end

#resourceCuprum::Rails::Resource (readonly)

Returns the resource to cache.

Returns:



64
65
66
# File 'lib/cuprum/rails/actions/middleware/associations/cache.rb', line 64

def resource
  @resource
end

Class Method Details

.define_strategy(klass) {|entity, name, value| ... } ⇒ Object

Defines a strategy for caching an association value.

Parameters:

  • klass (Class)

    the base class or module for matching entities.

Yields:

  • the strategy for caching the association value.

Yield Parameters:

  • entity (Object)

    the base entity.

  • name (String)

    the name of the association.

  • value (Object)

    the associated entity to cache.

Yield Returns:

  • (Object)

    the entity with cached assocation value.



40
41
42
# File 'lib/cuprum/rails/actions/middleware/associations/cache.rb', line 40

def define_strategy(klass, &block)
  (@strategies ||= STRATEGIES.dup)[klass] = block
end

.strategiesProc

Returns the defined strategies for caching association values.

Returns:

  • (Proc)

    the defined strategies for caching association values.



45
46
47
# File 'lib/cuprum/rails/actions/middleware/associations/cache.rb', line 45

def strategies
  (@strategies ||= STRATEGIES.dup).reverse_each
end