change position event code to use util

general purpose event fireing
can then be used for several event types
This commit is contained in:
Torsten Ruger 2018-05-31 19:05:53 +03:00
parent 2544bc5a85
commit 21a9c56ba9
2 changed files with 12 additions and 27 deletions

View File

@ -1,6 +1,10 @@
require "util/eventable"
module Risc
module Position
class ObjectPosition
include Util::Eventable
attr_reader :at , :object
def initialize( object, at)
@ -33,25 +37,6 @@ module Risc
@at = pos
true
end
# Register a handler position change event.
# The object calls position_changed on the handler object
#
# obj.position_changed( changed_position )
#
# @param [Object] object handling position_changed
def register_listener( handler)
@listeners << handler
end
def unregister_listener(handler)
@listeners.delete handler
end
# Trigger position change and pass self to position_changed
def trigger()
@listeners.each { |handler| handler.position_changed( self ) }
end
end
end
end

View File

@ -53,21 +53,21 @@ module Risc
@position = ObjectPosition.new(self,0)
end
def test_has_register
assert @position.register_listener(self)
assert @position.register_event(:position_changed , self)
end
def test_can_unregister
assert @position.register_listener(self)
assert @position.unregister_listener(self)
assert @position.register_event(:position_changed ,self)
assert @position.unregister_event(:position_changed ,self)
end
def test_fires
@position.register_listener(self)
@position.trigger
@position.register_event(:position_changed ,self)
@position.trigger(:position_changed , @position)
assert_equal @position , @trigger
end
def test_no_fire_after_unregister
assert @position.register_listener(self)
assert @position.unregister_listener(self)
@position.trigger
assert @position.register_event(:position_changed ,self)
assert @position.unregister_event(:position_changed ,self)
@position.trigger(:position_changed , @position)
assert_nil @trigger
end
def position_changed(pos)