module Isimud::ModelWatcher

ActiveModel mixin for sending model updates to a message server.

Constants

DEFAULT_EXCHANGE
IGNORED_COLUMNS

Public Instance Methods

isimud_sync() click to toggle source
# File lib/isimud/model_watcher.rb, line 82
def isimud_sync
  isimud_send_action_message(:update)
end
isimud_synchronize?() click to toggle source

Override to set conditions for synchronizing this instance with the server (default is always)

# File lib/isimud/model_watcher.rb, line 78
def isimud_synchronize?
  true
end

Protected Instance Methods

isimud_attribute_data() click to toggle source
# File lib/isimud/model_watcher.rb, line 106
def isimud_attribute_data
  attributes = isimud_watch_attributes || isimud_default_attributes
  attributes.inject(Hash.new) { |hsh, attr| hsh[attr] = send(attr); hsh }
end
isimud_default_attributes() click to toggle source
# File lib/isimud/model_watcher.rb, line 102
def isimud_default_attributes
  self.class.column_names - IGNORED_COLUMNS
end
isimud_model_watcher_exchange() click to toggle source
# File lib/isimud/model_watcher.rb, line 117
def isimud_model_watcher_exchange
  Isimud.model_watcher_exchange
end
isimud_model_watcher_routing_key(action) click to toggle source
# File lib/isimud/model_watcher.rb, line 125
def isimud_model_watcher_routing_key(action)
  [isimud_model_watcher_schema, isimud_model_watcher_type, action].join('.')
end
isimud_model_watcher_schema() click to toggle source
# File lib/isimud/model_watcher.rb, line 111
def isimud_model_watcher_schema
  Isimud.model_watcher_schema || if defined?(Rails)
                                          Rails.configuration.database_configuration[Rails.env]['database']
                                        end
end
isimud_model_watcher_type() click to toggle source
# File lib/isimud/model_watcher.rb, line 121
def isimud_model_watcher_type
  self.class.isimud_model_watcher_type
end
isimud_notify_created() click to toggle source
# File lib/isimud/model_watcher.rb, line 88
def isimud_notify_created
  isimud_send_action_message(:create)
end
isimud_notify_destroyed() click to toggle source
# File lib/isimud/model_watcher.rb, line 98
def isimud_notify_destroyed
  isimud_send_action_message(:destroy)
end
isimud_notify_updated() click to toggle source
# File lib/isimud/model_watcher.rb, line 92
def isimud_notify_updated
  changed_attrs = previous_changes.keys
  attributes    = isimud_watch_attributes || isimud_default_attributes
  isimud_send_action_message(:update) if (changed_attrs & attributes).any?
end
isimud_send_action_message(action) click to toggle source
# File lib/isimud/model_watcher.rb, line 129
def isimud_send_action_message(action)
  return unless Isimud.model_watcher_enabled? && isimud_synchronize?
  payload              = {
      schema:    isimud_model_watcher_schema,
      type:      isimud_model_watcher_type,
      action:    action,
      id:        id,
      timestamp: (updated_at || Time.now).utc
  }
  payload[:attributes] = isimud_attribute_data
  routing_key          = isimud_model_watcher_routing_key(action)
  log "Isimud::ModelWatcher#publish: exchange=#{isimud_model_watcher_exchange} routing_key=#{routing_key} id=#{id}"
  Isimud.client.publish(isimud_model_watcher_exchange, routing_key, payload.to_json)
end