Sha256: 4095f4dc81c1ef92f5eb0ae41db7f4917aec8e4b4685e37da8f42beddebd2a64
Contents?: true
Size: 1.71 KB
Versions: 1
Compression:
Stored size: 1.71 KB
Contents
module Vedeu # Provides a mechanism for storing and retrieving events by name. A single # name can contain many events. Also an event can trigger other events. # # @api private class Events # Initializes a new Events class. # # @param block [Proc] # @return [Events] def initialize(&block) @handlers = Hash.new { |hash, key| hash[key] = { events: [] } } instance_eval(&block) if block_given? end # @param object [] # @param block [Proc] # @return [] def add(object, &block) @self_before_instance_eval = eval('self', block.binding) instance_eval(&block) end # @see Vedeu::API#event def event(name, opts = {}, &block) handlers[name][:events] << Event.new(block, opts) handlers[name] end # @see Vedeu::API#unevent def unevent(name) handlers.delete_if { |k, v| k == name } end # Returns a collection of the names of all the registered events. # # @return [Array] def registered handlers.keys end # Returns a Boolean indicating whether the named event is registered. # # @return [TrueClass|FalseClass] def registered?(name) handlers.key?(name) end # @see Vedeu::API#trigger def trigger(name, *args) handlers[name][:events].each { |event| event.trigger(*args) } end # Remove all registered events. Used for testing purposes. # # @return [Hash] def reset @handlers = Hash.new { |hash, key| hash[key] = { events: [] } } end private attr_reader :handlers # @api private # @return [] def method_missing(method, *args, &block) @self_before_instance_eval.send(method, *args, &block) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
vedeu-0.1.19 | lib/vedeu/repositories/events.rb |