Permalink
Browse files

removed old lilmodel

  • Loading branch information...
1 parent 7d6bce8 commit 091aa8e75d523ce874deb45cdb407504877fb62f @gushov committed Oct 25, 2012
Showing with 0 additions and 344 deletions.
  1. +0 −175 lib/lilmodel.js
  2. +0 −169 test/lilmodel_test.js
View
175 lib/lilmodel.js
@@ -1,175 +0,0 @@
-/**
- * lilmodel
- * https://github.com/gushov/lilmodel
- *
- * Copyright (c) 2012 Gus Hovland
- * Licensed under the MIT license.
- */
-
-(function (exports) {
-
- "use strict";
-
- var sync;
- var model = {
- /**
- * Create instance of model
- */
- create: function (attrs) {
-
- var instance = Object.create(this, {
- attrs: {
- value: {},
- writable: true,
- enumerable: false,
- configurable : false
- }
- });
-
- return instance.init(attrs);
-
- },
-
- /**
- * Create a collection of models
- */
- collection: function (attrs) {
-
- var models = [];
-
- if (attrs && attrs.length) {
-
- models = attrs.map(function (attr) {
- return Object.getPrototypeOf(attr) === this ? attr : this.create(attr);
- }.bind(this));
-
- }
-
- return models;
-
- },
-
- /**
- * Model init function sets property values
- */
- init: function (attrs) {
-
- var config = this.config;
-
- Object.keys(config).forEach(function (prop) {
-
- var dfault = config && typeof config[prop] !== 'function' ? config[prop] : undefined;
- this[prop] = attrs && typeof attrs[prop] !== 'undefined' ? attrs[prop] : dfault;
-
- }.bind(this));
-
- return this;
- },
-
- /**
- * Method should be overridden to validate this.attrs values
- */
- validate: function () {
- return { isValid: true };
- },
-
- /**
- * If model is valid call sync save
- */
- save: function (next) {
-
- var validator = this.validate();
- var method = this._id ? 'update' : 'create';
-
- if (validator.isValid) {
- sync(method, this, next);
- } else {
- next(validator.error, this);
- }
-
- },
-
- /**
- * Call sync fetch
- */
- fetch: function (next) {
- sync('fetch', this, next);
- },
-
- /**
- * Call sync find
- */
- find: function (next) {
- sync('find', this, next);
- },
-
- /**
- * Call sync destroy
- */
- destroy: function (next) {
- sync('delete', this, next);
- },
-
- /**
- * Call next with self
- */
- next: function (next) {
- next(null, this);
- }
-
- };
-
- exports.sync = function (handler) {
-
- sync = function (method, model, next) {
-
- handler(method, model, function (err, result) {
-
- if (err) {
- next(err);
- } else if (result && typeof result.length === 'number') {
- next(err, model.collection(result));
- } else {
- next(err, model.create(result));
- }
-
- });
-
- };
-
- };
-
- exports.model = function (props) {
-
- var instance = Object.create(model);
-
- Object.keys(props).forEach(function (prop) {
-
- var isModel = typeof props[prop] === 'function';
-
- Object.defineProperty(instance, prop, {
-
- get: function () {
- return typeof this.attrs[prop] !== 'undefined' ? this.attrs[prop] : '';
- },
-
- set: function (value) {
- this.attrs[prop] = isModel ? props[prop](value) : value;
- }
-
- });
-
- });
-
- Object.defineProperty(instance, 'config', {
- value: props,
- writable: true,
- enumerable: false,
- configurable: false
- });
-
- return instance;
-
- };
-
-}(typeof exports === 'object' && exports || (this.lilmodel = {})));
View
169 test/lilmodel_test.js
@@ -1,169 +0,0 @@
-var sinon = require('sinon');
-var lilmodel = require('../lib/lilmodel');
-
-/*
- ======== A Handy Little Nodeunit Reference ========
- https://github.com/caolan/nodeunit
-
- Test methods:
- test.expect(numAssertions)
- test.done()
- Test assertions:
- test.ok(value, [message])
- test.equal(actual, expected, [message])
- test.notEqual(actual, expected, [message])
- test.deepEqual(actual, expected, [message])
- test.notDeepEqual(actual, expected, [message])
- test.strictEqual(actual, expected, [message])
- test.notStrictEqual(actual, expected, [message])
- test.throws(block, [error], [message])
- test.doesNotThrow(block, [error], [message])
- test.ifError(value)
-*/
-
-exports['test model'] = {
-
- setUp: function(done) {
-
- this.sync = sinon.stub().callsArgWith(2, null, { _id: 'jojojo', baby: { _id: 'lomlom' }});
- this.syncCol = sinon.stub().callsArgWith(2, null, [{ _id: 'jojojo' }, { _id: 'momomo' }]);
- this.syncWithCol = sinon.stub().callsArgWith(2, null, { _id: 'lomlom', babies: [{ _id: 'jojojo' }, { _id: 'momomo' }] });
- this.saveCb = sinon.spy();
- this.subModel = sinon.spy();
- this.validateTrue = sinon.stub().returns({ isValid: true });
- done();
-
- },
-
- tearDown: function(done) {
-
- delete this.sync;
- delete this.syncCol;
- delete this.saveCb;
- delete this.subModel;
- delete this.validateTrue;
- done();
-
- },
-
- 'create model': function(test) {
-
- var mongo = lilmodel.model({
- eats: 'mongobites',
- treats: 'nonnon'
- });
-
- var mgo = mongo.create();
-
- test.equal(mgo.eats, 'mongobites');
- test.equal(mgo.treats, 'nonnon');
-
- test.done();
- },
-
- 'save model': function(test) {
-
- lilmodel.sync(this.sync);
-
- var baby = lilmodel.model({
- _id: undefined,
- name: 'mar',
- jr: 'slim',
- l1: this.subModel,
- tooth: 'mine'
- });
-
- var papa = lilmodel.model({
- _id: undefined,
- name: 'booty',
- ham: 'full',
- bone: 'none',
- baby: baby.create.bind(baby)
- });
-
- papa.validate = this.validateTrue;
-
- var instance = papa.create();
-
- instance.init({ bone: 'love' });
- instance.ham = 'half';
- instance.baby = { jr: 'zozo' };
- instance.baby.l1 = { pasta: 'yes' };
- instance.save(this.saveCb);
-
- test.ok(this.validateTrue.calledOnce);
- test.equal(this.sync.firstCall.args[0], 'create');
- test.equal(this.sync.firstCall.args[1].name, 'booty');
- test.equal(this.sync.firstCall.args[1].ham, 'half');
- test.equal(this.sync.firstCall.args[1].bone, 'love');
- test.equal(this.sync.firstCall.args[1].baby.name, 'mar');
- test.equal(this.sync.firstCall.args[1].baby.jr, 'zozo');
- test.equal(this.sync.firstCall.args[1].baby.tooth, 'mine');
- test.equal(this.saveCb.firstCall.args[1]._id, 'jojojo');
- test.equal(this.saveCb.firstCall.args[1].baby._id, 'lomlom');
- test.ok(typeof this.subModel === 'function');
- test.equal(this.subModel.callCount, 5);
- test.ok(this.subModel.calledWith({ pasta: 'yes' }));
-
- test.done();
- },
-
- 'save with collection': function(test) {
-
- lilmodel.sync(this.syncWithCol);
-
- var baby = lilmodel.model({
- _id: undefined,
- name: 'zoe',
- jr: 'slim'
- });
-
- var papa = lilmodel.model({
- _id: undefined,
- name: 'booty',
- ham: 'full',
- babies: baby.collection.bind(baby)
- });
-
- var instance = papa.create();
- instance.init({ name: 'muji', babies: [{ name: 'juji' } , { name: 'fuji' }] });
- instance.save(this.saveCb);
-
- test.equal(this.syncWithCol.firstCall.args[1].name, 'muji');
- test.equal(this.syncWithCol.firstCall.args[1].ham, 'full');
- test.equal(this.syncWithCol.firstCall.args[1].babies[0].name, 'juji');
- test.equal(this.syncWithCol.firstCall.args[1].babies[0].jr, 'slim');
- test.equal(this.syncWithCol.firstCall.args[1].babies[1].name, 'fuji');
- test.equal(this.syncWithCol.firstCall.args[1].babies[1].jr, 'slim');
- test.equal(this.saveCb.firstCall.args[1]._id, 'lomlom');
- test.equal(this.saveCb.firstCall.args[1].babies[0]._id, 'jojojo');
- test.equal(this.saveCb.firstCall.args[1].babies[0].name, 'zoe');
- test.equal(this.saveCb.firstCall.args[1].babies[1]._id, 'momomo');
- test.equal(this.saveCb.firstCall.args[1].babies[1].name, 'zoe');
-
- test.done();
- },
-
- 'find models': function(test) {
-
- lilmodel.sync(this.syncCol);
-
- var baby = lilmodel.model({
- _id: undefined,
- jr: 'slim'
- });
-
- var instance = baby.create();
-
- instance.init({ jr: 'lalala' });
- instance.find(this.saveCb);
-
- test.equal(this.syncCol.firstCall.args[0], 'find');
- test.equal(this.syncCol.firstCall.args[1].jr, 'lalala');
- test.equal(this.saveCb.firstCall.args[1][0]._id, 'jojojo');
- test.equal(this.saveCb.firstCall.args[1][1]._id, 'momomo');
-
- test.done();
- }
-
-};

0 comments on commit 091aa8e

Please sign in to comment.