module Nova::Common::EventHandler::InstanceMethods
Instance methods.
Public Class Methods
Automatically binds the {Star} to itself on initialization.
# File lib/nova/common/event_handler.rb, line 67 def initialize(*) bind remote.new super() end
Public Instance Methods
@overload bind(bind)
Binds a copy of this object to a given object, running all events in the context of that object. @param bind [Object] the object to bind to. @return [Feature]
@overload bind
Returns the current bind. Creates an empty object if it doesn't exist. @return [Object]
# File lib/nova/common/event_handler.rb, line 145 def bind(bind = nil) if bind dup.bind!(bind) else @bind ||= Object.new end end
Binds this event handler to a given object, running all events in the context of that object.
@param bind [Object] the object to bind to. @return [self]
# File lib/nova/common/event_handler.rb, line 129 def bind!(bind) @bind = bind self end
Returns the event list.
@see ClassMethods#events
@return [Set<EventHandler::Event>]
# File lib/nova/common/event_handler.rb, line 76 def events @_events ||= self.class.events.dup end
Checks to see if an event can respond to the given name and options.
@param (see ClassMethods#has_event_with_options?
) @return (see ClassMethods#has_event_with_options?
)
# File lib/nova/common/event_handler.rb, line 85 def has_event_with_options?(name, options = {}) event = Event.new(name, options) event.type = :search events.dup.keep_if { |e| e.match? self, event }.to_a.first end
Calls {#run!}, but if it raises a [NoEventError] it returns it instead of raising it.
@see run!
@param (see run!
) @return [Object, NoEventError] the result of the event, or the exception.
# File lib/nova/common/event_handler.rb, line 116 def run(name, options = {}) run! name, options rescue NoEventError => e e end
Runs an event, with the given name and options.
@raise [NoEventError] when it can’t find the event. @param name [Symbol] the name of the event. @param options [Hash] the options to pass to the
event.
@return [Object] the result of the event.
# File lib/nova/common/event_handler.rb, line 98 def run!(name, options = {}) matched = has_event_with_options? name, options new_options = Metadata::Options.new(options) if matched matched.run(bind, new_options) else raise NoEventError, "Could not find event #{name}." end end