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?
<plug>
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.)
</plug>