Module: Omega::Client::Trackable

Included in:
Galaxy, Ship, SolarSystem, Station, User
Defined in:
lib/omega/client/mixins/trackable.rb

Overview

Include the Trackable module in classes to associate instances of the class w/ server side entities.

Examples:

class Ship
  include Trackable

  entity_type Manufactured::Ship
  get_method "manufactured::get_entity"
end

Ship.get('ship1')

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &bl) ⇒ Object

By default proxy all methods to underlying entity


37
38
39
# File 'lib/omega/client/mixins/trackable.rb', line 37

def method_missing(method_id, *args, &bl)
  self.entity.send(method_id, *args, &bl)
end

Instance Attribute Details

#entityObject

Entity being tracked.


29
30
31
# File 'lib/omega/client/mixins/trackable.rb', line 29

def entity
  @entity
end

#event_handlersObject

Returns the value of attribute event_handlers


41
42
43
# File 'lib/omega/client/mixins/trackable.rb', line 41

def event_handlers
  @event_handlers
end

Class Method Details

.included(base) ⇒ Object

See Also:


24
25
26
# File 'lib/omega/client/mixins/trackable.rb', line 24

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

.nodeObject

Centralized node to query / manage trackable entities


104
105
106
# File 'lib/omega/client/mixins/trackable.rb', line 104

def self.node
  @node ||= Omega::Client::Node.new
end

Instance Method Details

#clear_handlersObject

Clear all handlers


65
66
67
# File 'lib/omega/client/mixins/trackable.rb', line 65

def clear_handlers
  @event_handlers = nil
end

#clear_handlers_for(event) ⇒ Object

Clear handlers for the specified event


70
71
72
73
# File 'lib/omega/client/mixins/trackable.rb', line 70

def clear_handlers_for(event)
  @event_handlers[event] = [] if @event_handlers
  # TODO we should also remove rjr notification callback from @node
end

#handle(event, *args, &handler) ⇒ Object

Register handler for the specified event

Parameters:

  • event (Symbol)

    event to handle

  • args (Array<Object>)

    initialization parameters

  • handler (Callable)

    callback to invoke on event


48
49
50
51
52
53
54
55
56
57
# File 'lib/omega/client/mixins/trackable.rb', line 48

def handle(event, *args, &handler)
  esetup = self.class.event_setup[event]

  esetup.each { |cb|
    self.instance_exec(*args, &cb)
  } unless esetup.nil?

  @event_handlers ||= Hash.new() { |h,k| h[k] = [] }
  @event_handlers[event] << handler unless handler.nil?
end

#handles?(event) ⇒ Boolean

Return bool indicating if we're handling the specified event

Returns:

  • (Boolean)

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

def handles?(event)
  !@event_handlers.nil? && @event_handlers[event].size > 0
end

#nodeObject

Instance wrapper around Trackable.node


99
100
101
# File 'lib/omega/client/mixins/trackable.rb', line 99

def node
  Trackable.node
end

#raise_event(event, *eargs) ⇒ Object

Raise event on the entity, invoke registered handlers


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/omega/client/mixins/trackable.rb', line 76

def raise_event(event, *eargs)
  @event_handlers[event].each { |eh|
    begin
      eh.call self, *eargs
    rescue Exception, StandardError => e
      ::RJR::Logger.warn "err in #{id} #{event} handler:"
      ::RJR::Logger.warn "#{([e] + e.backtrace).join("\n")}"
    end

  } if @event_handlers && @event_handlers[event]

  # run :all callbacks
  @event_handlers[:all].each { |eh|
    begin
      eh.call self, *eargs
    rescue Exception, StandardError => e
      ::RJR::Logger.warn "err in #{id} 'all' handler:"
      ::RJR::Logger.warn "#{([e] + e.backtrace).join("\n")}"
    end
  } if @event_handlers && @event_handlers[:all]
end

#refreshObject

Refresh entity


32
33
34
# File 'lib/omega/client/mixins/trackable.rb', line 32

def refresh
  @entity = node.invoke(self.class.get_method, 'with_id', id)
end