Skip to content

Instantly share code, notes, and snippets.

@arnklint
Forked from weepy/gist:437682
Created November 1, 2010 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnklint/657928 to your computer and use it in GitHub Desktop.
Save arnklint/657928 to your computer and use it in GitHub Desktop.
sys: require("sys")
require.paths.unshift "/Users/jonahfox/src/mongoose"
mongoose: require('mongoose').Mongoose
mongoose.model 'User', {
properties: ['nick', 'pass', 'email', 'updated_at']
indexes: [[{ nick: 1 }, {unique: true}]]
setters: {}
getters: {}
methods: {
save: (callback) ->
@updated_at: +new Date()
@__super__ callback
validate: (callback) ->
@errors: []
@errors.push "bad nick" unless @nick and 3 < @nick.length < 16
@errors.push "bad password" if @pass and @pass.length < 4
@errors.push "bad email" unless @email and 5 < @email.length && @email.indexOf("@") > 0
User.find({nick: @nick}).first (user) =>
@errors.push "nick taken" if user
callback()
}
static: {
authorize: (nick, pass, callback) ->
@find({nick: nick, pass: pass}).first(callback)
}
}
db: mongoose.connect 'mongodb://localhost/testdb'
User: db.model 'User'
users: [
{ nick: "jonah", pass: "1234", email: "jonah@parkerfox.co.uk" }
{ nick: "bobby", pass: "1234", email: "jonah@parkerfox.co.uk" }
{ nick: "!!!!!", pass: "1234", email: "jonah@parkerfox.co.uk" }
{ nick: "!", pass: "1234", email: "jonah@parkerfox.co.uk" }
{ pass: "1234", email: "jonah@parkerfox.co.uk" }
{ nick: "!!3!!", pass: "1234", email: "parkerfox.co.uk" }
{ nick: "jonah", pass: "1234", email: "jonah@parkerfox.co.uk" }
]
for user in users
u: new User(user, true)
u.validate ->
if u.errors.length == 0
u.save()
sys.puts 'Saved!'
else
sys.puts u.errors.join(" ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment