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(attribute_names) { ... } ⇒ BackendResetter

Returns a new instance of BackendResetter.

Parameters:

  • attribute_names (Array<String>)

    Names of attributes whose backends should be reset

Yields:

  • Backend to reset as context for block

Raises:

  • (ArgumentError)

    if no block is provided.


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mobility/backend_resetter.rb', line 21

def initialize(attribute_names, &block)
  raise ArgumentError, "block required" unless block_given?
  names = attribute_names.map(&:to_sym)
  @model_reset_method = Proc.new do
    names.each do |name|
      if @mobility_backends && @mobility_backends[name]
        @mobility_backends[name].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


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mobility/backend_resetter.rb', line 35

def self.for(model_class)
  if Loaded::ActiveRecord && model_class < ::ActiveRecord::Base
    require "mobility/active_record/backend_resetter"
    ActiveRecord::BackendResetter
  elsif Loaded::ActiveRecord && model_class.ancestors.include?(::ActiveModel::Dirty)
    require "mobility/active_model/backend_resetter"
    ActiveModel::BackendResetter
  elsif Loaded::Sequel && model_class < ::Sequel::Model
    require "mobility/sequel/backend_resetter"
    Sequel::BackendResetter
  else
    self
  end
end