Module: Nova::Starbound::DefaultBehavior::Eventable::ClassMethods

Included in:
Nova::Starbound::DefaultBehavior
Defined in:
lib/nova/starbound/default_behavior/eventable.rb

Overview

Class methods.

API:

  • public

Instance Method Summary collapse

Instance Method Details

#handle(type, meth = nil) {|packet, protocol| ... } ⇒ void

Note:

If a block isn’t used, the method should accept two arguments (see yield params for the arguments).

This method returns an undefined value.

Defines how to handle a specific type of packet. The method of handling a packet can be defined in 3 ways: first, if a method name (second argument) is given, that method is used to handle the packet. Second, if a block is given, that block is used. And last, if neither of them are provided, the method name is assumed from the struct type and packet type in the format handle_<struct>_<packet>. The given method can and should be private.

Parameters:

  • a single key-value pair, with the key being the struct type and the value being the packet type.

  • (defaults to: nil)

    the method name to use when calling a block.

Yield Parameters:

API:

  • public



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nova/starbound/default_behavior/eventable.rb', line 31

def handle(type, meth = nil, &block)
  struct = type.keys.first
  packet = type.values.first

  proc = nil

  if meth
    proc = meth
  elsif block_given?
    proc = block
  else
    proc = :"handle_#{struct}_#{packet}"
  end

  handles[{struct => packet}] = proc
end

#handlesHash

The handles for events that are defined on this class.

Returns:

API:

  • public



51
52
53
# File 'lib/nova/starbound/default_behavior/eventable.rb', line 51

def handles
  @handles ||= {}
end