Class: QueueBus::Dispatch

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_key) ⇒ Dispatch

Returns a new instance of Dispatch.



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

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



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

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.



6
7
8
# File 'lib/queue_bus/dispatch.rb', line 6

def app_key
  @app_key
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



6
7
8
# File 'lib/queue_bus/dispatch.rb', line 6

def subscriptions
  @subscriptions
end

Instance Method Details

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



55
56
57
58
59
# File 'lib/queue_bus/dispatch.rb', line 55

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



49
50
51
52
53
# File 'lib/queue_bus/dispatch.rb', line 49

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



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

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

#sizeObject



13
14
15
# File 'lib/queue_bus/dispatch.rb', line 13

def size
  @subscriptions.size
end

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



17
18
19
# File 'lib/queue_bus/dispatch.rb', line 17

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

#subscription_matches(attributes) ⇒ Object



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

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