Created
October 27, 2011 23:02
-
-
Save plexus/1321157 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Watchable | |
attr_reader :watchers | |
def watchers | |
@watchers ||= Hash.new{|hsh,k| hsh[k]=[]} | |
end | |
def watch(&blk) | |
b=WatcherBuilder.new self | |
blk[b] if block_given? | |
b | |
end | |
def fireEvent(name, *args) | |
watchers[name].each {|w| w.call(*args)} | |
end | |
class WatcherBuilder | |
def initialize(watchable) | |
@watchable = watchable | |
end | |
def method_missing(name,&blk) | |
@watchable.watchers[name] << blk | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment