Skip to content

Instantly share code, notes, and snippets.

@creationix
Forked from brianleroux/person.js
Created October 14, 2010 02:50
Show Gist options
  • Save creationix/625450 to your computer and use it in GitHub Desktop.
Save creationix/625450 to your computer and use it in GitHub Desktop.
function Person() {
// properties and validations
this.attr(
{ id: Number, unique: true, nullable: false },
{ email: String, unique: true, nullable: false, min: 1, max: 55, format: '[a-b]' },
{ salt: String },
{ pswd: String },
{ active: Boolean, init: false },
{ tags: Array }
);
// helpful property declarations
this.timestamp();
// callbacks
this.creating({ before: poundSalt, after: emailActivationCode });
this.updating();
this.deleting();
// a private function to generate a unique salt for hashing the password, called in 'creating' callback
// testable therefore by the creating callback?
function poundSalt(obj) {
}
// emails an activation code
function emailActivationCode(obj) {
}
function forgotPassword(obj) {
}
// views, beautiful custom finders
this.view('tags', { map: function () {}, reduce: function () {} });
this.view('popular');
// public instance attributes
return {
// activates the user account
get active() {
},
set active() {
};
}
@brianleroux
Copy link

yeah I was trying to get rid of those this's ... so much visual entropy! there has to be a way to do this cleanly w/ today's syntax without resorting to backflips. Like the module pattern return statement but now things like email, etc are not publically accessible (tho maybe thats what we'd desire? I love fucking w/ my brain on these sorts of ideas.)

view's are just couchdb views... tho I suppose it would map well to any doc db

@isaacs
Copy link

isaacs commented Oct 14, 2010

If you return an obj, then 'this' is pointless.

@creationix
Copy link
Author

@isaacs only if you intend to use it as a constructor. If it's a function called by the framework with a custom scope, then this could hold meta functions.

@isaacs
Copy link

isaacs commented Oct 14, 2010

I guess... just seems like a lot of magic for a small amount of style. Why not just use js as-is? Its not so hard.

@creationix
Copy link
Author

I thought I was using javascript as-is. It's no more magic than Step.

@brianleroux
Copy link

'Its not so hard." sure comes off as a rather arrogant response to a non working psuedo dsl thought experiment!

@isaacs
Copy link

isaacs commented Oct 14, 2010

Sorry, not my intent to be arrogant, or overly critical of a sketch. Just that I have seen a lot of effort spent (by myself and others) on coding pattern non-problems in the past, and eventually came to the conclusion that just using ctor/prototype/new, in the standard way, while not the most pretty, is the least clever and surprising, and the easiest for a newcomer to jump into.

Yes, this is lovely:

function Foo () {
  // init
} << Bar << {
  foo : function () { .. }
}
var f = new Foo()
f instanceof Bar // true
f instanceof Foo // true
f.foo()

and with the hooks JavaScript gives you into the casting process with valueOf, it's entirely doable. (This is an extreme example, to be sure.) While lovely, and I'm sure it'd be fun to spend a saturday implementing it, it's not terribly useful.

Not that all sketches need to be diagrams. Some sketches can just be doodles, and that's perfectly ok. And occasionally something really neat comes out of the exploration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment