Module: ResqueBus::Subscriber::ClassMethods

Defined in:
lib/resque_bus/subscriber.rb

Instance Method Summary collapse

Instance Method Details

#app_keyObject



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

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

#application(app_key) ⇒ Object



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

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

#perform(attributes) ⇒ Object



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

def perform(attributes)
  ResqueBus.with_global_attributes(attributes) do
    sub_key = attributes["bus_rider_sub_key"]
    meth_key = sub_key.split(".").last
    resque_bus_execute(meth_key, attributes)
  end
end

#resque_bus_execute(key, attributes) ⇒ Object



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

def resque_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
# File 'lib/resque_bus/subscriber.rb', line 23

def subscribe(method_name, matcher_hash = nil)
  queue_name   = ::Resque.queue_from_class(self)
  queue_name ||= ::ResqueBus.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



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

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 = ::ResqueBus.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



38
39
40
# File 'lib/resque_bus/subscriber.rb', line 38

def transform(method_name)
  @transform = method_name
end