Sha256: 6fc47b3f048c37c4e4d62dbc01b8d3f2e9e42eb28459e476feff0d79944c8798

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

require 'securerandom'

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

            def create name
                name.downcase!

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

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

            def by_name name
                name.downcase!

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

                @data[:events][name]
            end
        end

        class Event
            attr_reader :name

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

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

                if @data[:callbacks].nil? then
                    @data[:callbacks] = Hash.new
                end

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

            def call(args=nil)
                if @data[:callbacks].nil? or @data[:callbacks].empty? then
                    return
                end

                @data[:callbacks].each { |uuid, proc|
                    proc.call(args)
                }
            end
        end
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fantasy-irc-0.1.1 lib/fantasy-irc/events.rb
fantasy-irc-0.1.0 lib/fantasy-irc/events.rb