-
-
Save arnklint/657928 to your computer and use it in GitHub Desktop.
This file contains 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
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