Class: Mobility::BackendResetter

Inherits:
Module
  • Object
show all
Defined in:
lib/mobility/backend_resetter.rb

Overview

Resets backend cache when reset events occur.

Examples:

Add trigger to call a method my_backend_reset_method on backend instance when reset event(s) occurs on model

resetter = Mobility::BackendResetter.for(MyModel).new(attributes) { my_backend_reset_method }
MyModel.include(resetter)

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) { ... } ⇒ BackendResetter

Returns a new instance of BackendResetter.

Parameters:

  • attributes (Array<String>)

    Attributes whose backends should be reset

Yields:

  • Backend to reset as context for block

Raises:

  • (ArgumentError)

    if no block is provided.



19
20
21
22
23
24
25
26
27
28
# File 'lib/mobility/backend_resetter.rb', line 19

def initialize(attributes, &block)
  raise ArgumentError, "block required" unless block_given?
  @model_reset_method = Proc.new do
    attributes.each do |attribute|
      if @mobility_backends && @mobility_backends[attribute]
        @mobility_backends[attribute].instance_eval &block
      end
    end
  end
end

Class Method Details

.for(model_class) ⇒ Object

Returns backend resetter class for model class

Parameters:

  • model_class (Class)

    Class of model to which backend resetter will be applied



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mobility/backend_resetter.rb', line 32

def self.for(model_class)
  if Loaded::ActiveRecord && model_class < ::ActiveRecord::Base
    ActiveRecord::BackendResetter
  elsif Loaded::ActiveRecord && model_class.ancestors.include?(::ActiveModel::Dirty)
    ActiveModel::BackendResetter
  elsif Loaded::Sequel && model_class < ::Sequel::Model
    Sequel::BackendResetter
  else
    self
  end
end