Class: Sponge::IRC::Listeners

Inherits:
Object
  • Object
show all
Defined in:
lib/sponge/irc/listeners.rb

Overview

Author

  • Lee Jarvis

Defined Under Namespace

Classes: Listener, ListenerExistsError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Listeners

Returns a new instance of Listeners.



30
31
32
33
34
35
36
# File 'lib/sponge/irc/listeners.rb', line 30

def initialize(client)
  @client = client
  @list = {}
  
  # Add the default PING reply
  add(:PING) {|i, m| i.pong(m.text) }
end

Instance Attribute Details

#clientObject (readonly)

The IRC::Client using this Listener List



28
29
30
# File 'lib/sponge/irc/listeners.rb', line 28

def client
  @client
end

Instance Method Details

#[](command) ⇒ Object



54
55
56
# File 'lib/sponge/irc/listeners.rb', line 54

def [](command)
  @list[command]
end

#add(*commands, &blk) ⇒ Object

Add a new Listener to the Listener List

  • See IRC::Client#on



40
41
42
43
44
45
46
47
# File 'lib/sponge/irc/listeners.rb', line 40

def add(*commands, &blk)
  commands.each do |command|
    command = command.to_s.downcase
    raise ListenerExistsError, "listener for #{command} already exists" if @list.key?(command)
    @list[command] = Listener.new(command, &blk)
  end
  commands
end

#allObject



75
76
77
# File 'lib/sponge/irc/listeners.rb', line 75

def all
  @list
end

#clearObject



62
63
64
# File 'lib/sponge/irc/listeners.rb', line 62

def clear
  @list.clear
end

#delete(command) ⇒ Object

Remove a listener from our list



50
51
52
# File 'lib/sponge/irc/listeners.rb', line 50

def delete(command)
  @list.delete(command.to_s.downcase)
end

#handle(message) ⇒ Object

Invoke #call on a Listener if it exists passing an IRC::Message



67
68
69
70
71
72
73
# File 'lib/sponge/irc/listeners.rb', line 67

def handle(message)
  raise "Not a IRC::Message" unless message.is_a?(Sponge::IRC::Message)
  command = message.command.to_s.downcase
  if @list.key?(command)
    @list[command].call(client.irc, message)
  end
end

#include?(k) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/sponge/irc/listeners.rb', line 58

def include?(k)
  @list.key?(k.to_s.downcase)
end