Sha256: 3b70f82a0f4801068ac1fea9a0467af2969fc2a89ff6dfb33d867b3e941990eb
Contents?: true
Size: 842 Bytes
Versions: 2
Compression:
Stored size: 842 Bytes
Contents
# Events are stored in the `@events` ivar. module Emittable # Register a handler for the given event name. # # obj.on(:foo) { puts "foo was called" } # # @param [String, Symbol] name event name # @return handler def on(name, &handler) @eventable ||= Hash.new { |hash, key| hash[key] = [] } @eventable[name] << handler handler end def off(name, handler) if @eventable and evts = @eventable[name] evts.delete handler end end # Trigger the given event name and passes all args to each handler # for this event. # # obj.trigger(:foo) # obj.trigger(:foo, 1, 2, 3) # # @param [String, Symbol] name event name to trigger def trigger(name, *args) @eventable ||= Hash.new { |hash, key| hash[key] = [] } @eventable[name].each { |handler| handler.call(*args) } end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
emittable-0.0.1 | lib/emittable.rb |
emittable-0.0.0 | lib/emittable.rb |