Hello, “observable.rb” is a variation on the observer pattern. It allows
multiple observers to register interest in the value of a variable, and
be notified when it changes. The main differences with Observer
(observer.rb) are that it is per-variable, not per-object, that it is
based on closures, and that variable state can be matched against
several patterns or values, each with a corresponding action.
Source, examples, and docs are at:
http://redshift.sourceforge.net/observable/
A simple example:
class Window
observable :visible # true or false
end
class WindowVisibleMenuCmd
def initialize window
window.when_visible true do
@checked = true
end
window.when_visible false do
@checked = false
end
@window = window
end
def do_command
@window.visible = !@checked
end
def inspect
"menu is #{"not " if not @checked}checked" +
" and window is #{"not " if not @window.visible}visible"
end
end
window = Window.new
window.visible = true
menu_cmd = WindowVisibleMenuCmd.new window
puts "Before command, " + menu_cmd.inspect
menu_cmd.do_command
puts "After command, " + menu_cmd.inspect
# Output:
# Before command, menu is checked and window is visible
# After command, menu is not checked and window is not visible