Sha256: 26568fb4cda9b94f2ddf234fad7f57292dfefc37bfe3be9f4cf60fb90dc7a54d

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

require "wisper"

module SyncMachine
  # Listen to changes in any model that could result in a change to the
  # published document, and enqueue a FindSubjectsWorker job when the change
  # occurs.
  class ChangeListener
    def self.inherited(base)
      base.cattr_accessor :model_syms
      base.subscribe
    end

    def self.listen_to_models(*model_syms)
      self.model_syms = model_syms
      model_syms.each do |model_sym|
        send(
          :alias_method,
          "update_#{model_sym}_successful".to_sym,
          :after_record_saved
        )
      end
    end

    def self.subscribe
      Wisper.subscribe(new)
    end

    def after_create(record)
      model_sym = record.class.name.underscore.to_sym
      return unless self.class.model_syms.include?(model_sym)
      after_record_saved(record)
    end

    def after_record_saved(record)
      return unless changed_keys(record).present?
      sync_module = SyncMachine.sync_module(self.class)
      finder_class = sync_module.const_get('FindSubjectsWorker')
      finder_class.perform_async(
        record.class.name,
        record_id_for_job(record.id),
        changed_keys(record),
        Time.now.iso8601
      )
    end

    private

    def changed_keys(record)
      orm_adapter.change_listener_changed_keys(record)
    end

    def record_id_for_job(record_id)
      orm_adapter.record_id_for_job(record_id)
    end

    def orm_adapter
      SyncMachine.orm_adapter
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sync_machine-0.5.0 lib/sync_machine/change_listener.rb
sync_machine-0.4.0 lib/sync_machine/change_listener.rb
sync_machine-0.3.0 lib/sync_machine/change_listener.rb
sync_machine-0.2.0 lib/sync_machine/change_listener.rb