Class: ActionSubscriber::Router
- Inherits:
-
Object
- Object
- ActionSubscriber::Router
- Defined in:
- lib/action_subscriber/router.rb
Constant Summary collapse
- DEFAULT_SETTINGS =
{ :acknowledgements => false, :durable => false, :exchange => "events", }.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #connection(name, settings) ⇒ Object
- #default_queue_for(route_settings) ⇒ Object
- #default_routes_for(subscriber, options = {}) ⇒ Object
- #default_routing_key_for(route_settings) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #local_application_name ⇒ Object
- #resource_name(route_settings) ⇒ Object
- #route(subscriber, action, options = {}) ⇒ Object
- #routes ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
15 16 17 |
# File 'lib/action_subscriber/router.rb', line 15 def initialize @current_connection_name = :default end |
Class Method Details
.draw_routes(&block) ⇒ Object
3 4 5 6 7 |
# File 'lib/action_subscriber/router.rb', line 3 def self.draw_routes(&block) router = self.new router.instance_eval(&block) router.routes end |
Instance Method Details
#connection(name, settings) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/action_subscriber/router.rb', line 19 def connection(name, settings) ::ActionSubscriber::RabbitConnection.setup_connection(name, settings) @current_connection_name = name yield @current_connection_name = :default end |
#default_queue_for(route_settings) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/action_subscriber/router.rb', line 34 def default_queue_for(route_settings) [ local_application_name, route_settings[:publisher], resource_name(route_settings), route_settings[:action].to_s, ].compact.join(".") end |
#default_routes_for(subscriber, options = {}) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/action_subscriber/router.rb', line 43 def default_routes_for(subscriber, = {}) = .merge({:connection_name => @current_connection_name}) subscriber.routes().each do |route| routes << route end end |
#default_routing_key_for(route_settings) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/action_subscriber/router.rb', line 26 def default_routing_key_for(route_settings) [ route_settings[:publisher], resource_name(route_settings), route_settings[:action].to_s, ].compact.join(".") end |
#local_application_name ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/action_subscriber/router.rb', line 50 def local_application_name @_local_application_name ||= begin local_application_name = case when ENV['APP_NAME'] then ENV['APP_NAME'].to_s.dup when defined?(::Rails) then ::Rails.application.class.parent_name.dup else raise "Define an application name (ENV['APP_NAME'])" end local_application_name.downcase end end |
#resource_name(route_settings) ⇒ Object
64 65 66 |
# File 'lib/action_subscriber/router.rb', line 64 def resource_name(route_settings) route_settings[:subscriber].name.underscore.gsub(/_subscriber/, "").to_s end |
#route(subscriber, action, options = {}) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/action_subscriber/router.rb', line 68 def route(subscriber, action, = {}) route_settings = DEFAULT_SETTINGS.merge(:connection_name => @current_connection_name).merge().merge(:subscriber => subscriber, :action => action) route_settings[:routing_key] ||= default_routing_key_for(route_settings) route_settings[:queue] ||= default_queue_for(route_settings) routes << Route.new(route_settings) end |
#routes ⇒ Object
75 76 77 |
# File 'lib/action_subscriber/router.rb', line 75 def routes @routes ||= [] end |