Module: Communicator

Defined in:
lib/communicator.rb,
lib/communicator/version.rb

Defined Under Namespace

Modules: ActiveRecordIntegration Classes: Client, FakeLogger, InboundMessage, MissingCredentials, OutboundMessage, ReceiverUnknown, Server

Constant Summary collapse

VERSION =
'0.2.0'

Class Method Summary collapse

Class Method Details

.loggerObject



24
25
26
# File 'lib/communicator.rb', line 24

def logger
  defined?(Rails) ? Rails.logger : Communicator::FakeLogger
end

.receiver_for(source) ⇒ Object

Tries to find the receiver for given source, raising Communicator::ReceiverUnknown on failure



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/communicator.rb', line 48

def receiver_for(source)
  return receivers[source] if receivers[source]
  
  # If not found in the first place, maybe the class just isn't loaded yet and
  # thus hasn't registered - let's require all models and try again
  if defined?(Rails)
    Dir[File.join(Rails.root, 'app/models/**/*.rb')].each {|model| require model}
    return receivers[source] if receivers[source]
  end
  
  # When everything else fails, just throw an exception...
  raise Communicator::ReceiverUnknown.new("No receiver registered for '#{source}'")
end

.receiversObject

Hash containing all receivers



29
30
31
# File 'lib/communicator.rb', line 29

def receivers
  @recevers ||= {}.with_indifferent_access
end

.register_receiver(target, source, options = {}) ⇒ Object

Register a given class as a receiver from source (underscored name). Will then mix in the instance methods from Communicator::ActiveRecord::InstanceMethods so message processing and publishing functionality is included



36
37
38
39
40
41
42
43
44
# File 'lib/communicator.rb', line 36

def register_receiver(target, source, options={})
  receivers[source] = target
  target.send(:include, Communicator::ActiveRecordIntegration::InstanceMethods)
  
  target.skip_remote_attributes(*options[:except]) if options[:except]
  Communicator.logger.info "Registered #{target} as receiver for messages from #{source}"
  
  target
end