Module: Communicator

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

Defined Under Namespace

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

Constant Summary collapse

VERSION =
'0.2.3'

Class Method Summary collapse

Class Method Details

.loggerObject



32
33
34
# File 'lib/communicator.rb', line 32

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



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/communicator.rb', line 56

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



37
38
39
# File 'lib/communicator.rb', line 37

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



44
45
46
47
48
49
50
51
52
# File 'lib/communicator.rb', line 44

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