Sha256: 363f3086e8029cbd347e9fe2fa3f314031c089145e9fc2aa53f15532bd753e81

Contents?: true

Size: 1.7 KB

Versions: 4

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

# Creates a DSL for apps to define their blocks to run for event_types

module QueueBus
  # A Dispatch object can be used to declare an application along with it's various subscriptions.
  class Dispatch
    attr_reader :app_key, :subscriptions

    def initialize(app_key)
      @app_key = Application.normalize(app_key)
      @subscriptions = SubscriptionList.new
    end

    def size
      @subscriptions.size
    end

    def subscribe(key, matcher_hash = nil, &block)
      dispatch_event('default', key, matcher_hash, block)
    end

    # allows definitions of other queues
    def method_missing(method_name, *args, &block)
      if args.size == 1 && block
        dispatch_event(method_name, args[0], nil, block)
      elsif args.size == 2 && block
        dispatch_event(method_name, args[0], args[1], block)
      else
        super
      end
    end

    def execute(key, attributes)
      sub = subscriptions.key(key)
      if sub
        sub.execute!(attributes)
      else
        # TODO: log that it's not there
      end
    end

    def subscription_matches(attributes)
      out = subscriptions.matches(attributes)
      out.each do |sub|
        sub.app_key = app_key
      end
      out
    end

    def dispatch_event(queue, key, matcher_hash, block)
      # if not matcher_hash, assume key is a event_type regex
      matcher_hash ||= { 'bus_event_type' => key }
      add_subscription("#{app_key}_#{queue}", key, '::QueueBus::Rider', matcher_hash, block)
    end

    def add_subscription(queue_name, key, class_name, matcher_hash = nil, block)
      sub = Subscription.register(queue_name, key, class_name, matcher_hash, block)
      subscriptions.add(sub)
      sub
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
queue-bus-0.12.0 lib/queue_bus/dispatch.rb
queue-bus-0.11.0 lib/queue_bus/dispatch.rb
queue-bus-0.10.0 lib/queue_bus/dispatch.rb
queue-bus-0.9.1 lib/queue_bus/dispatch.rb