Sha256: 522530ac86c431d8530e335a571d6b3788b89006884412341d5aa40efa83fd35

Contents?: true

Size: 1.5 KB

Versions: 13

Compression:

Stored size: 1.5 KB

Contents

require "dry-container"

module Hermes
  class EventHandler
    attr_reader :container, :consumer_builder
    private     :container, :consumer_builder

    def initialize(container: Dry::Container.new, consumer_builder: Hermes::DependenciesContainer["consumer_builder"])
      @container = container
      @consumer_builder = consumer_builder
    end

    def handle_events(&block)
      instance_exec(&block)
    end

    def handle(event_class, with:, async: true, rpc: false, consumer_config: -> {})
      handler = with
      options = {
        async: async,
        rpc: rpc,
        consumer_config: consumer_config
      }
      consumer = build_consumer_for_event(event_class, consumer_config)

      Registration.new(handler, consumer, options).tap do |registration|
        container.register(event_class, registration)
      end
    end

    def registration_for(event_class)
      container.resolve(event_class)
    end

    private

    def build_consumer_for_event(event_class, consumer_config)
      consumer_builder.build(event_class, consumer_config: consumer_config)
    end

    class Registration
      attr_reader :handler, :consumer, :options

      def initialize(handler, consumer, options = {})
        @handler = handler
        @consumer = consumer
        @options = options
      end

      def async?
        options.fetch(:async) == true
      end

      def rpc?
        options.fetch(:rpc) == true
      end

      def consumer_config
        optons.fetch(:consumer_config)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
hermes-rb-0.9.1 lib/hermes/event_handler.rb
hermes-rb-0.9.0 lib/hermes/event_handler.rb
hermes-rb-0.8.0 lib/hermes/event_handler.rb
hermes-rb-0.7.5 lib/hermes/event_handler.rb
hermes-rb-0.7.4 lib/hermes/event_handler.rb
hermes-rb-0.7.3 lib/hermes/event_handler.rb
hermes-rb-0.7.2 lib/hermes/event_handler.rb
hermes-rb-0.7.1 lib/hermes/event_handler.rb
hermes-rb-0.7.0 lib/hermes/event_handler.rb
hermes-rb-0.6.1 lib/hermes/event_handler.rb
hermes-rb-0.6.0 lib/hermes/event_handler.rb
hermes-rb-0.5.0 lib/hermes/event_handler.rb
hermes-rb-0.4.0 lib/hermes/event_handler.rb