Sha256: 4ac9fca54e873c45c3c7d6e35ce039d323847049793440daed8cb76c853a1ef0

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require "rabbit"
require "rabbit/receiving"
require "rabbit/event_handler"

module Rabbit::Receiving::HandlerResolver
  UnsupportedEvent = Class.new(StandardError)

  class << self
    def handler_for(message)
      @handler_cache ||= Hash.new do |cache, (group_id, event)|
        handler = unmemoized_handler_for(group_id, event)
        cache[[group_id, event]] = handler if Rabbit.config.environment == :production
        handler
      end

      @handler_cache[[message.group_id, message.event]]
    end

    private

    def unmemoized_handler_for(group_id, event)
      handler = if Rabbit.config.handler_resolver_callable.is_a?(Proc)
                  Rabbit.config.handler_resolver_callable.call(group_id, event)
                else
                  name = "rabbit/handler/#{group_id}/#{event}".camelize
                  name.safe_constantize
                end

      if handler && handler < Rabbit::EventHandler
        handler
      else
        raise UnsupportedEvent, "#{event.inspect} event from #{group_id.inspect} group is not " \
                                "supported, it requires a #{name.inspect} class inheriting from " \
                                "\"Rabbit::EventHandler\" to be defined"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rabbit_messaging-1.1.0 lib/rabbit/receiving/handler_resolver.rb
rabbit_messaging-1.0.0 lib/rabbit/receiving/handler_resolver.rb
rabbit_messaging-0.15.0 lib/rabbit/receiving/handler_resolver.rb