Sha256: 262828bfa487b6c422e2b09fcc0f77a2ab95f9e4beccf07080b7483532fef539

Contents?: true

Size: 1.71 KB

Versions: 15

Compression:

Stored size: 1.71 KB

Contents

module Mobility
=begin

Resets backend cache when reset events occur.

@example 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 Mobility::ActiveRecord::BackendResetter
@see Mobility::ActiveModel::BackendResetter
@see Mobility::Sequel::BackendResetter

=end
  class BackendResetter < Module
    # @param [Array<String>] attribute_names Names of attributes whose backends should be reset
    # @yield Backend to reset as context for block
    # @raise [ArgumentError] if no block is provided.
    def initialize(attribute_names, &block)
      raise ArgumentError, "block required" unless block_given?
      @model_reset_method = Proc.new do
        attribute_names.each do |name|
          if @mobility_backends && @mobility_backends[name]
            @mobility_backends[name].instance_eval(&block)
          end
        end
      end
    end

    # Returns backend resetter class for model class
    # @param [Class] model_class Class of model to which backend resetter will be applied
    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
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
mobility-0.4.3 lib/mobility/backend_resetter.rb
mobility-0.4.2 lib/mobility/backend_resetter.rb
mobility-0.4.1 lib/mobility/backend_resetter.rb
mobility-0.4.0 lib/mobility/backend_resetter.rb
mobility-0.3.6 lib/mobility/backend_resetter.rb
mobility-0.3.5 lib/mobility/backend_resetter.rb
mobility-0.3.4 lib/mobility/backend_resetter.rb
mobility-0.3.3 lib/mobility/backend_resetter.rb
mobility-0.3.2 lib/mobility/backend_resetter.rb
mobility-0.3.1 lib/mobility/backend_resetter.rb
mobility-0.3.0 lib/mobility/backend_resetter.rb
mobility-0.2.3 lib/mobility/backend_resetter.rb
mobility-0.2.2 lib/mobility/backend_resetter.rb
mobility-0.2.1 lib/mobility/backend_resetter.rb
mobility-0.2.0 lib/mobility/backend_resetter.rb