Module: QueueBus::Subscriber::ClassMethods

Defined in:
lib/queue_bus/subscriber.rb

Instance Method Summary collapse

Instance Method Details

#app_keyObject



14
15
16
17
18
19
20
21
# File 'lib/queue_bus/subscriber.rb', line 14

def app_key
  return @app_key if @app_key
  @app_key = ::QueueBus.default_app_key
  return @app_key if @app_key
  # module or class_name
  val = self.name.to_s.split("::").first
  @app_key = ::QueueBus::Util.underscore(val)
end

#application(app_key) ⇒ Object



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

def application(app_key)
  @app_key = ::QueueBus::Application.normalize(app_key)
end

#perform(attributes) ⇒ Object



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

def perform(attributes)
  ::QueueBus.with_global_attributes(attributes) do
    sub_key = attributes["bus_rider_sub_key"]
    meth_key = sub_key.split(".").last
    queue_bus_execute(meth_key, attributes)
  end
end

#queue_bus_execute(key, attributes) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/queue_bus/subscriber.rb', line 51

def queue_bus_execute(key, attributes)
  args = attributes
  args = send(@transform, attributes) if @transform
  args = [args] unless args.is_a?(Array)
  if self.respond_to?(:subscriber_with_attributes)
    me = self.subscriber_with_attributes(attributes)
  else
    me = self.new
  end
  me.send(key, *args)
end

#subscribe(method_name, matcher_hash = nil) ⇒ Object



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

def subscribe(method_name, matcher_hash = nil)
  queue_name   = nil
  queue_name ||= self.instance_variable_get(:@queue) || (self.respond_to?(:queue) && self.queue)
  queue_name ||= ::QueueBus.default_queue
  queue_name ||= "#{app_key}_default"
  subscribe_queue(queue_name, method_name, matcher_hash)
end

#subscribe_queue(queue_name, method_name, matcher_hash = nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/queue_bus/subscriber.rb', line 31

def subscribe_queue(queue_name, method_name, matcher_hash = nil)
  klass = self
  matcher_hash ||= {"bus_event_type" => method_name}
  sub_key = "#{self.name}.#{method_name}"
  dispatcher = ::QueueBus.dispatcher_by_key(app_key)
  dispatcher.add_subscription(queue_name, sub_key, klass.name.to_s, matcher_hash, lambda{ |att| klass.perform(att) })
end

#transform(method_name) ⇒ Object



39
40
41
# File 'lib/queue_bus/subscriber.rb', line 39

def transform(method_name)
  @transform = method_name
end