Module: Omega::Client::TrackState

Included in:
Ship
Defined in:
lib/omega/client/mixins/track_state.rb

Overview

Include TrackState in a Trackable class to register handlers to be invoked on various custom user defined states of the server side entity.

Note is up to the developer to seperate register events and handlers to update the local entity from the server side state

Examples:

class Ship
  include Trackable
  include TrackState
  entity_type Manufactured::Ship
  get_method "manufactured::get_entity"

  server_state :cargo_full,
    :check => lambda { |e| e.cargo_full?       },
    :on    => lambda { |e| e.offload_resources },
    :off   => lambda { |e|}

  def offload_resources
    # ...
  end
end

See Also:

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#statesObject

States the entity currently has


43
44
45
# File 'lib/omega/client/mixins/track_state.rb', line 43

def states
  @states
end

Class Method Details

.included(base) ⇒ Object

The class methods below will be defined on the class including this module

See Also:


38
39
40
# File 'lib/omega/client/mixins/track_state.rb', line 38

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#off_state(state, &bl) ⇒ Object

Register handler to be invoked when the entity leaves the specified state


60
61
62
63
# File 'lib/omega/client/mixins/track_state.rb', line 60

def off_state(state, &bl)
  @off_state_callbacks[state] ||= []
  @off_state_callbacks[state] << bl
end

#on_state(state, &bl) ⇒ Object

Register handler to be invoked when the entity enters the specified state


50
51
52
53
# File 'lib/omega/client/mixins/track_state.rb', line 50

def on_state(state, &bl)
  @on_state_callbacks[state]  ||= []
  @on_state_callbacks[state]  << bl
end

#set_state(state) ⇒ Object

This method is invoked by the tracker module when the entity enters the specified state


72
73
74
75
76
77
78
# File 'lib/omega/client/mixins/track_state.rb', line 72

def set_state(state)
  return if @states.include?(state) # TODO add flag to disable this check
  @states << state
  @on_state_callbacks[state].each { |cb|
    instance_exec self, &cb
  } if @on_state_callbacks.has_key?(state)
end

#unset_state(state) ⇒ Object

This method is invoked by the tracker module when the entity leaves the specified state


84
85
86
87
88
89
90
91
# File 'lib/omega/client/mixins/track_state.rb', line 84

def unset_state(state)
  if(@states.include?(state))
    @states.delete(state)
    @off_state_callbacks[state].each { |cb|
      instance_exec self, &cb
    } if @off_state_callbacks.has_key?(state)
  end
end