Skip to content

Instantly share code, notes, and snippets.

@shesek
Created March 2, 2012 10:24
Show Gist options
  • Save shesek/1957562 to your computer and use it in GitHub Desktop.
Save shesek/1957562 to your computer and use it in GitHub Desktop.
CoffeeScript class decorators
# 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