Sha256: 31fa360a71d439588c5c4d3a634f4331442ff623be9fd130aad2a2eb9b2fd60f

Contents?: true

Size: 1.01 KB

Versions: 25

Compression:

Stored size: 1.01 KB

Contents

module Elastics
  module ClassProxy
    module ModelSyncer

      attr_accessor :synced

      def sync(*synced)
        # Elastics::ActiveModel has its own way of syncing, and a Elastics::ModelSyncer cannot be synced by itself
        raise ArgumentError, %(You cannot elastics.sync(self) #{context}.) \
              if synced.any?{|s| s == context} && !context.include?(Elastics::ModelIndexer)
        synced.each do |s|
          s == context || s.is_a?(Symbol) || s.is_a?(String) || raise(ArgumentError, "self, string or symbol expected, got #{s.inspect}")
        end
        @synced ||= []
        @synced  |= synced
        add_callbacks
      end

      def add_callbacks
        context.class_eval do
          raise NotImplementedError, "the class #{self} must implement :after_save and :after_destroy callbacks" \
                unless respond_to?(:after_save) && respond_to?(:after_destroy)
          after_save    { elastics.sync }
          after_destroy { elastics.sync }
        end
      end

    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
elastics-models-1.3.3 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.3.2 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.3.1 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.3.0 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.2.1 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.2.0 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.1.11 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.1.10 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.1.9 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.1.8 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.1.7 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.1.6 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.1.5 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.1.4 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.1.3 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.1.2 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.1.1 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.1.0 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.0.10 lib/elastics/class_proxy/model_syncer.rb
elastics-models-1.0.9 lib/elastics/class_proxy/model_syncer.rb