Skip to content

Instantly share code, notes, and snippets.

@plexus
Created October 27, 2011 23:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plexus/1321157 to your computer and use it in GitHub Desktop.
Save plexus/1321157 to your computer and use it in GitHub Desktop.
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