Sha256: 9202fb5ed77329ec71be600d9b4c69596952406a6d3d8f46ae72781b32c86e81

Contents?: true

Size: 1.52 KB

Versions: 24

Compression:

Stored size: 1.52 KB

Contents

module Elastics
  module InstanceProxy
    class ModelSyncer

      attr_reader :instance, :class_elastics

      def initialize(instance)
        @instance       = instance
        @class_elastics = instance.class.elastics
      end

      def sync(*trail)
        return if trail.include?(uid) || class_elastics.synced.nil?
        trail << uid
        class_elastics.synced.each do |synced|
          case
          # sync self
          when synced == instance.class
            sync_self
          # sync :author, :comments
          # works for all association types, if the instances have a #elastics proxy
          when synced.is_a?(Symbol)
            to_sync = instance.send(synced)
            if to_sync.respond_to?(:each)
              to_sync.each { |s| s.elastics.sync(*trail) }
            else
              to_sync.elastics.sync(*trail)
            end
          # sync 'blog'
          # polymorphic: use this form only if you want to sync any specific parent type but not all
          when synced.is_a?(String)
            next unless synced == parent_instance.elastics.type
            parent_instance.elastics.sync(*trail)
          else
            raise ArgumentError, "self, string or symbol expected, got #{synced.inspect}"
          end
        end
      end

      def uid
        @uid ||= [instance.class, instance.id].join('-')
      end

      def refresh_index
        class_elastics.refresh_index
      end

      def sync_self
        # nothing to sync, since a ModelSyncer cannot sync itselfs
      end

    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

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