Class: Motel::Callbacks::ChangedStrategy

Inherits:
Omega::Server::Callback show all
Defined in:
lib/motel/callbacks/changed_strategy.rb

Overview

Defines a Omega::Server::Callback to only invoke callback if a location's movement strategy is changed

This callback will be invoked with the current location (with new movement strategy) as well as the old movement strategy

TODO this is only invoked when the movement strategy class changes, not when the strategy stays the same but attributes are updated. Would be nice to also support the later

Instance Attribute Summary collapse

Attributes inherited from Omega::Server::Callback

#endpoint_id, #event_type, #handler, #only_if, #rjr_event

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Omega::Server::Callback

#should_invoke?

Constructor Details

#initialize(args = {}, &block) ⇒ ChangedStrategy

Motel::Callbacks::ChangedStrategy initializer

Parameters:

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

    hash of options to initialize callback with

Options Hash (args):

  • :orig_ms,'orig_ms' (Integer)

    original movement strategy


39
40
41
42
43
44
# File 'lib/motel/callbacks/changed_strategy.rb', line 39

def initialize(args = {}, &block)
  attr_from_args args, :orig_ms => nil

  @only_if = proc { |*args| self.check_strategy(*args) }
  super(args)
end

Instance Attribute Details

#orig_msObject

original strategy which to compare against


22
23
24
# File 'lib/motel/callbacks/changed_strategy.rb', line 22

def orig_ms
  @orig_ms
end

Class Method Details

.json_create(o) ⇒ Object


66
67
68
69
# File 'lib/motel/callbacks/changed_strategy.rb', line 66

def self.json_create(o)
  callback = new(o['data'])
  return callback
end

Instance Method Details

#invoke(loc) ⇒ Object

Override Omega::Server::Callback#invoke, call original then update orig_ms


47
48
49
50
# File 'lib/motel/callbacks/changed_strategy.rb', line 47

def invoke(loc)
  super(loc, @orig_ms)
  @orig_ms = loc.ms
end

#to_json(*a) ⇒ Object

Convert callback to json representation and return it


58
59
60
61
62
63
64
# File 'lib/motel/callbacks/changed_strategy.rb', line 58

def to_json(*a)
  {
    'json_class' => self.class.name,
    'data'       =>
      { :endpoint_id => @endpoint_id, :orig_ms => @orig_ms }
  }.to_json(*a)
end

#to_sObject

Convert callback to human readable string and return it


53
54
55
# File 'lib/motel/callbacks/changed_strategy.rb', line 53

def to_s
  "(#{@orig_ms.class})"
end