Skip to content

Instantly share code, notes, and snippets.

@clyfe
Created June 16, 2011 20:23
Show Gist options
  • Save clyfe/1030168 to your computer and use it in GitHub Desktop.
Save clyfe/1030168 to your computer and use it in GitHub Desktop.
CoffeeScript on a Pony
class Base
@include: (module) ->
@::[k] = v for k, v of module::
@has_many: (rel, opts) ->
@::[rel] = -> console.log rel
@tableName: ->
@_tableName ?= @toString().match(/function ([^\(]+)/)[1]
@find: (id) ->
console.log "SELECT * FROM #{@tableName()} WHERE id = #{id}"
save: (opts = null) ->
tableName = @constructor.tableName()
if @id?
console.log "UPDATE #{tableName} SET k='v' WHERE id = #{id}"
else
console.log "INSERT INTO #{tableName} VALUES ('v')"
class Trashable
trash: -> @trashed = true
class User extends Base
@include Trashable
@has_many "roles"
user = new User()
user.trash()
console.log user.trashed # true
user.roles() # roles
User.find(1) # SELECT * FROM User WHERE id = 1
user.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment