1998-01-16 15:13:05 +03:00
|
|
|
# Observable Mixin
|
|
|
|
#
|
|
|
|
# Observers must respond to update
|
|
|
|
|
|
|
|
module Observable
|
|
|
|
def add_observer(observer)
|
|
|
|
@observer_peers = [] unless @observer_peers
|
|
|
|
unless defined? observer.update
|
|
|
|
raise NameError, "observer needs to respond to `update'"
|
|
|
|
end
|
|
|
|
@observer_peers.push observer
|
|
|
|
end
|
|
|
|
def delete_observer(observer)
|
|
|
|
@observer_peers.delete observer if @observer_peers
|
|
|
|
end
|
|
|
|
def delete_observers
|
|
|
|
@observer_peers.clear if @observer_peers
|
|
|
|
end
|
|
|
|
def count_observers
|
|
|
|
if @observer_peers
|
|
|
|
@observer_peers.size
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
def changed(state=true)
|
1998-01-16 15:13:05 +03:00
|
|
|
@observer_state = state
|
|
|
|
end
|
|
|
|
def changed?
|
|
|
|
@observer_state
|
|
|
|
end
|
|
|
|
def notify_observers(*arg)
|
1999-01-20 07:59:39 +03:00
|
|
|
if @observer_state
|
|
|
|
if @observer_peers
|
|
|
|
for i in @observer_peers
|
|
|
|
i.update(*arg)
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
@observer_state = false
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|