Module: Nova::Common::EventHandler::InstanceMethods

Includes:
ClassMethods
Included in:
Star
Defined in:
lib/nova/common/event_handler.rb

Overview

Instance methods.

Instance Method Summary collapse

Methods included from ClassMethods

#has_event?, #on

Instance Method Details

#bind(bind) ⇒ Feature #bindObject

Overloads:

  • #bind(bind) ⇒ Feature

    Binds a copy of this object to a given object, running all events in the context of that object.

    Parameters:

    • bind (Object)

      the object to bind to.

    Returns:

    • (Feature)
  • #bindObject

    Returns the current bind. Creates an empty object if it doesn’t exist.

    Returns:

    • (Object)


145
146
147
148
149
150
151
# File 'lib/nova/common/event_handler.rb', line 145

def bind(bind = nil)
  if bind
    dup.bind!(bind)
  else
    @bind ||= Object.new
  end
end

#bind!(bind) ⇒ self

Binds this event handler to a given object, running all events in the context of that object.

Parameters:

  • bind (Object)

    the object to bind to.

Returns:

  • (self)


129
130
131
132
# File 'lib/nova/common/event_handler.rb', line 129

def bind!(bind)
  @bind = bind
  self
end

#eventsSet<EventHandler::Event>

Returns the event list.

Returns:

See Also:



76
77
78
# File 'lib/nova/common/event_handler.rb', line 76

def events
  @_events ||= self.class.events.dup
end

#has_event_with_options?(name, options = {}) ⇒ nil, Event

Checks to see if an event can respond to the given name and options.

Parameters:

  • name (Symbol)

    the name of the event.

  • options (Hash) (defaults to: {})

    the options for the event.

Returns:



85
86
87
88
89
# 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

#initializeObject

Automatically binds the Star to itself on initialization.



67
68
69
70
# File 'lib/nova/common/event_handler.rb', line 67

def initialize(*)
  bind remote.new
  super()
end

#run(name, options = {}) ⇒ Object, NoEventError

Calls #run!, but if it raises a [NoEventError] it returns it instead of raising it.

exception.

Parameters:

  • name (Symbol)

    the name of the event.

  • options (Hash) (defaults to: {})

    the options to pass to the event.

Returns:

  • (Object, NoEventError)

    the result of the event, or the

See Also:



116
117
118
119
120
121
# File 'lib/nova/common/event_handler.rb', line 116

def run(name, options = {})
  run! name, options

rescue NoEventError => e
  e
end

#run!(name, options = {}) ⇒ Object

Runs an event, with the given name and options.

Parameters:

  • name (Symbol)

    the name of the event.

  • options (Hash) (defaults to: {})

    the options to pass to the event.

Returns:

  • (Object)

    the result of the event.

Raises:



98
99
100
101
102
103
104
105
106
107
# File 'lib/nova/common/event_handler.rb', line 98

def run!(name, options = {})
  matched = has_event_with_options? name, options
  new_options = ::Options.new(options)

  if matched
    matched.run(bind, new_options)
  else
    raise NoEventError, "Could not find event #{name}."
  end
end