Sha256: 3f43c5a4d6d66f4232da235149dcfd25c5b2f19fbb0b943a46fcce53a5b6abec

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

module Fantasy
    class Plugins
        attr_reader :plugins

        def initialize
            @plugins = {}
        end

        def add plugin
            @plugins[plugin.name] = plugin
            puts "#{plugin.name} = #{plugin}"
        end

        def load name
            Kernel::load "plugins/#{name}.rb"
        end

        def command command, data, args
            if not args.nil? then
                args = args.split(' ')
            end
            @plugins.values.each do |plugin|
                puts "#{plugin}"
                plugin.handle! command, data, args
            end
        end
    end
end

class Plugin
    attr_reader :name

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

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

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

        return false
    end

    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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fantasy-irc-0.2.0 lib/fantasy-irc/plugins.rb
fantasy-irc-0.1.2 lib/fantasy-irc/plugins.rb
fantasy-irc-0.1.1 lib/fantasy-irc/plugins.rb
fantasy-irc-0.1.0 lib/fantasy-irc/plugins.rb