Sha256: 3151dee070da355a9b65da544e152b168d6404cd7079953ab1ac9d6a5b55ea08

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

require 'securerandom'

module Fantasy
    module Event
        class Factory
            def initialize
                puts "Initializing new Event::Factory #{self}"
                @events = Hash.new
            end

            def create name
                name.downcase!

                if not @events[name].nil? then
                    return @events[name]
                    # TODO: log warning
                end

                @events[name] = Event.new(name)
            end

            def by_name name
                name.downcase!

                if not @events[name] then
                    raise "Tried to access unknown event \"#{name}\" in Event::Factory \"#{self}\""
                end

                @events[name]
            end
        end

        class Event
            attr_reader :name

            def initialize(name)
                puts "New Event with name #{name}"
                @name = name
                @callbacks = Hash.new
            end

            def register(&callback)
                uuid = SecureRandom.uuid()

                @callbacks[uuid] = callback
                puts "#{self}: registered callback #{callback} with uuid #{uuid}."
            end

            def call(args=nil)
                if @callbacks.empty? then
                    return
                end

                @callbacks.each { |uuid, proc|
                    proc.call(*args)
                }
            end
        end
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fantasy-irc-0.3.0 lib/fantasy-irc/events.rb