Class: Chasqui::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/chasqui/subscriber.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue, channel) ⇒ Subscriber

Returns a new instance of Subscriber.



11
12
13
14
# File 'lib/chasqui/subscriber.rb', line 11

def initialize(queue, channel)
  @queue = queue
  @channel = channel
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



57
58
59
60
61
62
63
# File 'lib/chasqui/subscriber.rb', line 57

def method_missing(method, *args, &block)
  if @self_before_instance_eval
    @self_before_instance_eval.send method, *args, &block
  else
    super
  end
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



9
10
11
# File 'lib/chasqui/subscriber.rb', line 9

def channel
  @channel
end

#current_eventObject

Returns the value of attribute current_event.



8
9
10
# File 'lib/chasqui/subscriber.rb', line 8

def current_event
  @current_event
end

#queueObject (readonly)

Returns the value of attribute queue.



9
10
11
# File 'lib/chasqui/subscriber.rb', line 9

def queue
  @queue
end

#redisObject

Returns the value of attribute redis.



8
9
10
# File 'lib/chasqui/subscriber.rb', line 8

def redis
  @redis
end

Instance Method Details

#call_handler(pattern, *args) ⇒ Object



42
43
44
# File 'lib/chasqui/subscriber.rb', line 42

def call_handler(pattern, *args)
  send "handler__#{pattern.to_s}", *args
end

#evaluate(&block) ⇒ Object



46
47
48
49
# File 'lib/chasqui/subscriber.rb', line 46

def evaluate(&block)
  @self_before_instance_eval = eval "self", block.binding
  instance_eval &block
end

#matching_handler_patterns_for(event_name) ⇒ Object



36
37
38
39
40
# File 'lib/chasqui/subscriber.rb', line 36

def matching_handler_patterns_for(event_name)
  handler_patterns.select do |pattern|
    pattern =~ event_name
  end
end

#on(event_name, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/chasqui/subscriber.rb', line 16

def on(event_name, &block)
  pattern = pattern_for_event event_name

  if handler_patterns.include? pattern
    raise HandlerAlreadyRegistered.new "handler already registered for event: #{event_name}"
  else
    handler_patterns << pattern
    define_handler_method pattern, &block
  end
end

#perform(redis_for_worker, event) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/chasqui/subscriber.rb', line 27

def perform(redis_for_worker, event)
  self.redis = redis_for_worker
  self.current_event = event

  matching_handler_patterns_for(event['event']).each do |pattern|
    call_handler pattern, *event['data']
  end
end