I use Observable module in one of my projects. I noticed something
strange and don’t know if it is intentional.
The add_observer method adds (surprise!) an observer for an object, but
it does not check if this observer is not already registered for this
object. Thus it is possible to add the same observer multiple times for
the same Observable object. In my library I do some deletion in "update"
method - if the observer is registered twice, the deletion is done
twice, which is undesirable. There is no method to add observer
conditionally, that is, to check if it is already added!
Is there any way to add an observer if and only if it wasn’t already
registered for a particular Observable object?
···
–
Marek Janukowicz
Marek Janukowicz wrote:
…
Is there any way to add an observer if and only if it wasn’t already
registered for a particular Observable object?
I don't know about Observable in the standard library, but in my
observable library on RAA, observers are registered in a hash, so
registering twice has no effect.
However, it is not quite a plug-in replacement for the standard library,
though it allows the same functionality. For instance, you declare an
attr (or method) to be observable, and updates happen when the writer of
the attr is called, rather when a notify method is called. Also, it
allows you to register for changes based on pattern matching and case
matching (===). An example:
class User
observable :nickname
end
class NicknameChecker
def initialize(user)
user.when_nickname /\Afred\z/i do
puts “I’ll call you Fred from now on.”
end
user.when_nickname /\Agumby\z/i do
puts “I refuse to address anyone as Gumby.”
end
end
end
(There are better examples in the RAA package, which, in retrospect,
should probably have been named ObservableAttribute or something.)
Very nice - but in this particular case I need something working more
like standard module (with manually called notify_observers method).
···
On Sat, 24 May 2003 10:37:38 +0900, Joel VanderWerf wrote:
Is there any way to add an observer if and only if it wasn’t already
registered for a particular Observable object?
I don't know about Observable in the standard library, but in my
observable library on RAA, observers are registered in a hash, so
registering twice has no effect.
However, it is not quite a plug-in replacement for the standard library,
though it allows the same functionality. For instance, you declare an
attr (or method) to be observable, and updates happen when the writer of
the attr is called, rather when a notify method is called. Also, it
allows you to register for changes based on pattern matching and case
matching (===). An example:
class User
observable :nickname
end
class NicknameChecker
def initialize(user)
user.when_nickname /\Afred\z/i do
puts “I’ll call you Fred from now on.”
end
user.when_nickname /\Agumby\z/i do
puts “I refuse to address anyone as Gumby.”
end
end
end
(There are better examples in the RAA package, which, in retrospect,
should probably have been named ObservableAttribute or something.)
–
Marek Janukowicz