class ActionSubscriber::Router

Constants

DEFAULT_SETTINGS

Public Class Methods

draw_routes(&block) click to toggle source
# File lib/action_subscriber/router.rb, line 3
def self.draw_routes(&block)
  router = self.new
  router.instance_eval(&block)
  router.routes
end
new() click to toggle source
# File lib/action_subscriber/router.rb, line 15
def initialize
  @current_threadpool_name = :default
end

Public Instance Methods

connection(name, settings, &block) click to toggle source
# File lib/action_subscriber/router.rb, line 19
def connection(name, settings, &block)
  ::ActionSubscriber.print_deprecation_warning("setting up a threadpool for #{name} instead of a new connection")
  threadpool(name, settings, &block)
end
default_queue_for(route_settings) click to toggle source
# File lib/action_subscriber/router.rb, line 39
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 = {}) click to toggle source
# File lib/action_subscriber/router.rb, line 48
def default_routes_for(subscriber, options = {})
  options = options.merge({:threadpool_name => @current_threadpool_name})
  subscriber.routes(options).each do |route|
    routes << route
  end
end
default_routing_key_for(route_settings) click to toggle source
# File lib/action_subscriber/router.rb, line 31
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() click to toggle source
# File lib/action_subscriber/router.rb, line 55
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
                               if ::Rails.application.class.respond_to?(:module_parent_name)
                                 ::Rails.application.class.module_parent_name.dup
                               else
                                 ::Rails.application.class.parent_name.dup
                               end
                             else
                               raise "Define an application name (ENV['APP_NAME'])"
                             end
    local_application_name.downcase
  end
end
resource_name(route_settings) click to toggle source
# File lib/action_subscriber/router.rb, line 73
def resource_name(route_settings)
  route_settings[:subscriber].name.underscore.gsub(/_subscriber/, "").to_s
end
route(subscriber, action, options = {}) click to toggle source
# File lib/action_subscriber/router.rb, line 77
def route(subscriber, action, options = {})
  route_settings = DEFAULT_SETTINGS.merge(:threadpool_name => @current_threadpool_name).merge(options).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() click to toggle source
# File lib/action_subscriber/router.rb, line 84
def routes
  @routes ||= []
end
threadpool(name, settings) { || ... } click to toggle source
# File lib/action_subscriber/router.rb, line 24
def threadpool(name, settings)
  ::ActionSubscriber::ThreadPools.setup_threadpool(name, settings)
  @current_threadpool_name = name
  yield
  @current_threadpool_name = :default
end