class RabbitFeed::EventRouting

Attributes

catch_all_application[R]
named_applications[R]

Public Class Methods

new() click to toggle source
# File lib/rabbit_feed/event_routing.rb, line 99
def initialize
  @named_applications = {}
end

Public Instance Methods

accept_from(name, &block) click to toggle source
# File lib/rabbit_feed/event_routing.rb, line 103
def accept_from(name, &block)
  if name == :any
    accept_from_any_application(&block)
  else
    accept_from_named_application(name, &block)
  end
end
accepted_routes() click to toggle source
# File lib/rabbit_feed/event_routing.rb, line 111
def accepted_routes
  routes = named_applications.values.flat_map(&:accepted_routes)
  routes += catch_all_application.accepted_routes if catch_all_application.present?
  routes
end
handle_event(event) click to toggle source
# File lib/rabbit_feed/event_routing.rb, line 117
def handle_event(event)
  application = find_application event
  raise RoutingError, "No routing defined for application with name: #{event.application} for events named: #{event.name}" unless application.present?
  application.handle_event event
end

Private Instance Methods

accept_from_any_application(&block) click to toggle source
# File lib/rabbit_feed/event_routing.rb, line 132
def accept_from_any_application(&block)
  raise ConfigurationError, 'Routing has already been defined for the application catch-all: :any' if catch_all_application.present?
  application = Application.new '*'
  application.instance_eval(&block)
  @catch_all_application = application
end
accept_from_named_application(name, &block) click to toggle source
# File lib/rabbit_feed/event_routing.rb, line 125
def accept_from_named_application(name, &block)
  raise ConfigurationError, "Routing has already been defined for the application with name: #{name}" if named_applications.key? name
  application = Application.new name
  application.instance_eval(&block)
  named_applications[application.name] = application
end
find_application(event) click to toggle source
# File lib/rabbit_feed/event_routing.rb, line 139
def find_application(event)
  candidate_applications = [named_applications[event.application], catch_all_application].compact
  candidate_applications.detect do |application|
    application.handles_event? event
  end
end