Class: Msngr::Receiver

Inherits:
Object
  • Object
show all
Defined in:
lib/msngr/receiver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern = /.+/) ⇒ Receiver

Initializes a new Receiver.

Parameters:

  • pattern (Regexp) (defaults to: /.+/)

    the pattern to listen for.


29
30
31
32
33
34
# File 'lib/msngr/receiver.rb', line 29

def initialize(pattern = /.+/)
  @pattern                  = pattern
  @on_message_callbacks     = []
  @on_unsubscribe_callbacks = []
  @mutex                    = Mutex.new
end

Instance Attribute Details

#on_message_callbacksArray<Proc> (readonly)

Array of Procs to invoke when a message is received.

Returns:

  • (Array<Proc>)

15
16
17
# File 'lib/msngr/receiver.rb', line 15

def on_message_callbacks
  @on_message_callbacks
end

#on_unsubscribe_callbacksArray<Proc> (readonly)

Array of Procs to invoke when unsubscribed from a Messenger.

Returns:

  • (Array<Proc>)

21
22
23
# File 'lib/msngr/receiver.rb', line 21

def on_unsubscribe_callbacks
  @on_unsubscribe_callbacks
end

#patternString (readonly)

Event pattern to match.

Returns:

  • (String)

9
10
11
# File 'lib/msngr/receiver.rb', line 9

def pattern
  @pattern
end

Instance Method Details

#on_message(&block) ⇒ Object

Define a callback that invokes on each received message.

Examples:

receiver.on_message do |message|
  puts "Message Received: #{message}"
end

Parameters:

  • block (Proc)

45
46
47
# File 'lib/msngr/receiver.rb', line 45

def on_message(&block)
  @mutex.synchronize { @on_message_callbacks << block }
end

#on_unsubscribe(&block) ⇒ Object

Define a callback that invokes when unsubscribed from a Messenger.

Examples:

receiver.on_unsubscribe do |messenger|
  puts "Unsubscribed From: #{messenger}"
end

Parameters:

  • block (Proc)

58
59
60
# File 'lib/msngr/receiver.rb', line 58

def on_unsubscribe(&block)
  @mutex.synchronize { @on_unsubscribe_callbacks << block }
end