Class: Nova::Common::EventHandler::Event

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/nova/common/event_handler/event.rb

Overview

This represents an event that can be run. It may also represent an event that will be run, and is being used to match the events in the set. Basically, an event is defined when :on is called on a Star, and an Event is created. It is added to an event list, which is a set. Later, when that class is instantized, and an event is ran, an Event is created and the set is searched for a match to that Event.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options, block = nil) ⇒ Event

Initialize the event.

Parameters:

  • name (Symbol)

    the name of the event to respond to.

  • options (Hash)

    the options of the event.

  • block (Proc) (defaults to: nil)

    the defining block of the event.

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.



60
61
62
63
64
65
# File 'lib/nova/common/event_handler/event.rb', line 60

def initialize(name, options, block = nil)
  @name = name
  @options = options
  @block = block
  @type = :definition
end

Instance Attribute Details

#blockProc (readonly)

The block defined for the event. Should be able to accept one parameter, or less.

Returns:

  • (Proc)


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

def block
  @block
end

#nameSymbol (readonly)

The name of the event.

Returns:

  • (Symbol)


20
21
22
# File 'lib/nova/common/event_handler/event.rb', line 20

def name
  @name
end

#optionsHash (readonly)

The options defined at compile time for the event.

Returns:

  • (Hash)


25
26
27
# File 'lib/nova/common/event_handler/event.rb', line 25

def options
  @options
end

#typeSymbol

The type of Nova::Common::EventHandler::Event this is. By default, this value is :definition, but when using this event to find another event, its value should be :search.

Returns:

  • (Symbol)


38
39
40
# File 'lib/nova/common/event_handler/event.rb', line 38

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Numeric

Compares this event to another event. If the argument isn’t an Nova::Common::EventHandler::Event, it is compared to the name of the event. If it is, the event’s names and options are compared.

Parameters:

  • other (Event, Object)

    the event to compare to.

Returns:

  • (Numeric)

    the comparison result.



73
74
75
76
77
# File 'lib/nova/common/event_handler/event.rb', line 73

def <=>(other)
  return @name == other unless Event === other

  (@name <=> other.name) + (@options <=> other.options)
end

#match?(star, event) ⇒ Boolean

Whether or not this event matches another event, to see if it can be ran using this event definition.

Parameters:

  • star (Class, #platform)
  • event (Symbol)

Returns:

  • (Boolean)


107
108
109
110
# File 'lib/nova/common/event_handler/event.rb', line 107

def match?(star, event)
  event.name == name &&
  check_platform_requirement(star) && check_options_requirements(event.options)
end

#run(context, options = {}) ⇒ Object?

Runs the event, calling the block with the given options.

Parameters:

  • context (Object)

    the context to run it in.

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

    the options.

Returns:

  • (Object, nil)


84
85
86
87
88
89
90
91
92
# File 'lib/nova/common/event_handler/event.rb', line 84

def run(context, options = {})
  #@block.call(options)

  if @options[:defaults]
    options = @options[:defaults].merge(options)
  end

  context.instance_exec(options, &@block)
end

#search?Boolean

Whether or not this event is a search event.

Returns:

  • (Boolean)


97
98
99
# File 'lib/nova/common/event_handler/event.rb', line 97

def search?
  type == :search
end