Class: QueueBus::Dispatch

Inherits:
Object
  • Object
show all
Defined in:
lib/queue_bus/dispatch.rb

Overview

A Dispatch object can be used to declare an application along with it’s various subscriptions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_key) ⇒ Dispatch

Returns a new instance of Dispatch.



10
11
12
13
# File 'lib/queue_bus/dispatch.rb', line 10

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

allows definitions of other queues



24
25
26
27
28
29
30
31
32
# File 'lib/queue_bus/dispatch.rb', line 24

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

Instance Attribute Details

#app_keyObject (readonly)

Returns the value of attribute app_key.



8
9
10
# File 'lib/queue_bus/dispatch.rb', line 8

def app_key
  @app_key
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



8
9
10
# File 'lib/queue_bus/dispatch.rb', line 8

def subscriptions
  @subscriptions
end

Instance Method Details

#add_subscription(queue_name, key, class_name, matcher_hash = nil, block) ⇒ Object



57
58
59
60
61
# File 'lib/queue_bus/dispatch.rb', line 57

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

#dispatch_event(queue, key, matcher_hash, block) ⇒ Object



51
52
53
54
55
# File 'lib/queue_bus/dispatch.rb', line 51

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

#execute(key, attributes) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/queue_bus/dispatch.rb', line 34

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

#sizeObject



15
16
17
# File 'lib/queue_bus/dispatch.rb', line 15

def size
  @subscriptions.size
end

#subscribe(key, matcher_hash = nil, &block) ⇒ Object



19
20
21
# File 'lib/queue_bus/dispatch.rb', line 19

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

#subscription_matches(attributes) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/queue_bus/dispatch.rb', line 43

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