Sha256: 1c28887f715183a8b987615065e83644d2d8e926bb00101d340f0ecc136b9f0b
Contents?: true
Size: 1.64 KB
Versions: 2
Compression:
Stored size: 1.64 KB
Contents
require "active_support/json" require "active_support/core_ext/object/json" 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 do [] end end def self.define_method_matching_wisper_event(event) define_method(event) do |subject| find_subjects_async(subject) end 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.listen_to_wisper_events(*events) events.each do |event| define_method_matching_wisper_event(event) 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 orm_adapter.sufficient_changes_to_find_subjects?(record) find_subjects_async(record) end private def find_subjects_async(record) sync_module = SyncMachine.sync_module(self.class) finder_class = sync_module.const_get('FindSubjectsWorker') finder_class.perform_async_for_record(record) end def orm_adapter SyncMachine.sync_module(self.class).orm_adapter end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
sync_machine-1.4.0 | lib/sync_machine/change_listener.rb |
sync_machine-1.3.0 | lib/sync_machine/change_listener.rb |