Created
March 2, 2012 10:24
-
-
Save shesek/1957562 to your computer and use it in GitHub Desktop.
CoffeeScript class decorators
This file contains hidden or 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
# Just playing around with some ideas... | |
interface = (names...) -> (klass) -> throw new Error "Missing #{n}" for n in names when not klass::[n]?; klass | |
extender = (props) -> (klass) -> klass::[k]=v for k,v of props; klass | |
watchable = interface 'watch', 'unwatch' | |
writable = interface 'write' | |
evented = extender | |
on: (event, callback) -> do stuff | |
trigger: (name, args...) -> do stuff | |
writable class User | |
write: -> write stuff | |
evented class Post | |
# Gets the `on` and `trigger` methods copied to its prototype | |
writable watchable class Blog | |
write: -> write stuff | |
watch: -> watch stuff | |
# Missing 'unwatch' causes an error | |
writable evented class Category | |
write: -> write stuff | |
# Also gets `on` and `trigger` methods |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment