Module: Nova::Common::EventHandler::ClassMethods

Included in:
InstanceMethods, Star
Defined in:
lib/nova/common/event_handler.rb

Overview

Class methods.

API:

  • public

Instance Method Summary collapse

Instance Method Details

#eventsSet<EventHandler::Event>

A list of all of the events defined in the class.

Returns:

API:

  • public



16
17
18
# File 'lib/nova/common/event_handler.rb', line 16

def events
  @_events ||= Set.new
end

#has_event?(name) ⇒ nil, Event

Note:

Even if the event with the given name may exist, the event may still not exist if the options don’t match. If you want to make sure that the event will respond to the name and options, check #has_event_with_options?.

Checks to see if an event with the given name exists.

Parameters:

  • the name of the event.

Returns:

API:

  • public



44
45
46
# File 'lib/nova/common/event_handler.rb', line 44

def has_event?(name)
  events.dup.keep_if { |e| e.name == name }.to_a.first
end

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

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

Parameters:

  • the name of the event.

  • (defaults to: {})

    the options for the event.

Returns:

API:

  • public



54
55
56
57
58
# File 'lib/nova/common/event_handler.rb', line 54

def has_event_with_options?(name, options = {})
  event = Event.new(name, options)
  event.type = :search
  events.dup.keep_if { |e| e.match? Star.new, event }.to_a.first
end

#on(event, options = {}) {|options| ... } ⇒ Event Also known as: to

Creates an event, adds it to the list, and returns it.

Parameters:

  • the name of the event.

  • (defaults to: {})

    the options for the event, when creating it.

Options Hash (options):

  • :on (Symbol)

    the platform the event is written for. If this doesn’t match the current platform, the event is never run. Must be in the results of Remote::Fake::Platform#types (or remote’s definition of platform). Can also be :for.

  • :requires (Symbol, Array<Symbol>)

    what options the event requires when being run. The value will be matched against the keys of the hash passed, and if the hash doesn’t contain all of the values, the event doesn’t match. Can also be :require.

  • :defaults (Hash)

    the default values to be used when running the event. Merged into the given options such that the given options overwrite the defaults.

Yield Parameters:

  • options (Hash)

    the options for the event when ran. The contents of the options hash is defined at the time when ran.

Returns:

  • the event.

API:

  • public



30
31
32
# File 'lib/nova/common/event_handler.rb', line 30

def on(event, options = {})
  events << Event.new(event, options, Proc.new)
end