Class: Plugin

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

Direct Known Subclasses

HackerSays, Magic8Ball

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Plugin

Returns a new instance of Plugin.



50
51
52
53
# File 'lib/fantasy-irc/plugins.rb', line 50

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



48
49
50
# File 'lib/fantasy-irc/plugins.rb', line 48

def name
  @name
end

Instance Method Details

#handle(pattern, &block) ⇒ Object



55
56
57
# File 'lib/fantasy-irc/plugins.rb', line 55

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

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fantasy-irc/plugins.rb', line 69

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}"
            begin
                block.call data, args
            rescue Exception => e
                puts "#{block} failed with Exception #{e}"
                puts e.backtrace
            end

            break
        end
    end
end

#handles?(command) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
# File 'lib/fantasy-irc/plugins.rb', line 59

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

    return false
end