Sha256: a5c934764727df4e5c34726299f63925042a874664aa010b17de8e64afe80bcf

Contents?: true

Size: 1.54 KB

Versions: 14

Compression:

Stored size: 1.54 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>] attributes Attributes whose backends should be reset
    # @yield Backend to reset as context for block
    # @raise [ArgumentError] if no block is provided.
    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

    # 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
        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
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
mobility-0.1.20 lib/mobility/backend_resetter.rb
mobility-0.1.19 lib/mobility/backend_resetter.rb
mobility-0.1.18 lib/mobility/backend_resetter.rb
mobility-0.1.17 lib/mobility/backend_resetter.rb
mobility-0.1.16 lib/mobility/backend_resetter.rb
mobility-0.1.15 lib/mobility/backend_resetter.rb
mobility-0.1.14 lib/mobility/backend_resetter.rb
mobility-0.1.13 lib/mobility/backend_resetter.rb
mobility-0.1.12 lib/mobility/backend_resetter.rb
mobility-0.1.11 lib/mobility/backend_resetter.rb
mobility-0.1.10 lib/mobility/backend_resetter.rb
mobility-0.1.7 lib/mobility/backend_resetter.rb
mobility-0.1.6 lib/mobility/backend_resetter.rb
mobility-0.1.5 lib/mobility/backend_resetter.rb