Class: Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/fantasy-irc/plugins.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Plugin

Returns a new instance of Plugin.



35
36
37
38
# File 'lib/fantasy-irc/plugins.rb', line 35

def initialize name
    @name = name
    @handlers = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



33
34
35
# File 'lib/fantasy-irc/plugins.rb', line 33

def name
  @name
end

Instance Method Details

#handle(pattern, &block) ⇒ Object



40
41
42
# File 'lib/fantasy-irc/plugins.rb', line 40

def handle pattern, &block
    @handlers[pattern] = block
end

#handle!(command, data, args = []) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/fantasy-irc/plugins.rb', line 54

def handle! command, data, args=[]
    puts "trying to handle #{command} with #{data} and #{args}"
    @handlers.each do |pattern, block|
        if command.match(pattern) then
            puts "#{block} handles #{command}"
            break block.call data, args
        end
    end
end

#handles?(command) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
# File 'lib/fantasy-irc/plugins.rb', line 44

def handles? command
    @handlers.keys.each do |pattern|
        if command.match pattern then
            return true
        end
    end

    return false
end