Sha256: 84e795188a85d98453e7d236a90bfd962ede8322ae716d9bc0814f3885867dec

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

require "active_support/core_ext/class"
require "active_support/core_ext/string"
require "mongoid"
require "sync_machine/change_listener"
require "sync_machine/ensure_publication"
require "sync_machine/ensure_publication/deduper"
require "sync_machine/ensure_publication/publication_history"
require "sync_machine/ensure_publication_worker"
require "sync_machine/find_subjects_worker"
require "sync_machine/version"
require "wisper/mongoid"

# A mini-framework for intelligently publishing complex model changes to an
# external API..
module SyncMachine
  def self.extended(base)
    base.mattr_accessor :subject_sym
  end

  def self.sync_module(child_const)
    child_const.name.split(/::/).first.constantize
  end

  def subject(subject_sym)
    self.subject_sym = subject_sym
  end

  def subject_class
    subject_sym.to_s.camelize.constantize
  end

  def setup
    define_payload_class unless const_defined?('Payload')
    const_get('ChangeListener').subscribe
  end

  # :reek:TooManyStatements is unavoidable with this sort of dynamic class
  # definition
  def define_payload_class
    collection_name = name.underscore + '_payloads'
    payload_class = Class.new do
      include Mongoid::Document
      store_in collection: collection_name

      field :body, type: Hash
      field :generated_at, type: Time
      field :subject_id, type: String

      validates :generated_at, presence: true
      validates :subject_id, presence: true, uniqueness: true
    end
    const_set('Payload', payload_class)
  end
end

Wisper::Mongoid.extend_all

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sync_machine-0.1.0 lib/sync_machine.rb