Skip to content

Commit

Permalink
move to istanbul for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Swatinem committed Mar 26, 2014
1 parent 8537da0 commit 153fe20
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 139 deletions.
3 changes: 1 addition & 2 deletions .gitignore
@@ -1,7 +1,6 @@
node_modules
components
lib-cov
coverage.html
coverage
npm-debug.log
*.tgz
*.patch
Expand Down
20 changes: 20 additions & 0 deletions .jshintrc
@@ -0,0 +1,20 @@
{
"curly": false,
"eqeqeq": true,
"unused": true,
"undef": true,
"esnext": true,
"laxbreak": true,
"smarttabs": true,
"node": true,
"browser": true,
"expr": true,
"globals" : {
"describe" : false,
"it" : false,
"before" : false,
"beforeEach" : false,
"after" : false,
"afterEach" : false
}
}
3 changes: 1 addition & 2 deletions .npmignore
@@ -1,7 +1,6 @@
node_modules
components
lib-cov
coverage.html
coverage
npm-debug.log
*.tgz
*.patch
Expand Down
21 changes: 8 additions & 13 deletions Makefile
@@ -1,16 +1,11 @@
test:
NODE_ENV=test ./node_modules/.bin/mocha --harmony
test: lint
NODE_ENV=test node --harmony ./node_modules/.bin/istanbul cover \
./node_modules/mocha/bin/_mocha

lib-cov: clean-cov
./node_modules/.bin/jscoverage lib lib-cov
lint:
-./node_modules/.bin/jshint ./test ./index.js

clean-cov:
rm -rf lib-cov
test-coveralls: test
-cat ./coverage/lcov.info | ./node_modules/.bin/coveralls

test-cov: lib-cov
MODEL_COV=1 NODE_ENV=test ./node_modules/.bin/mocha --harmony --reporter html-cov 1> coverage.html

test-coveralls: lib-cov
MODEL_COV=1 NODE_ENV=test ./node_modules/.bin/mocha --harmony --reporter mocha-lcov-reporter | ./node_modules/.bin/coveralls

.PHONY: test test-cov test-coveralls clean-cov
.PHONY: test lint test-coveralls
4 changes: 2 additions & 2 deletions component.json
Expand Up @@ -4,9 +4,9 @@
"version": "0.0.4",
"description": "transparent ES5 models",
"keywords": [],
"main": "lib/index.js",
"main": "index.js",
"scripts": [
"lib/index.js"
"index.js"
],
"dependencies": {
"component/emitter": "*"
Expand Down
113 changes: 110 additions & 3 deletions index.js
@@ -1,3 +1,110 @@
module.exports = process.env.MODEL_COV
? require('./lib-cov')
: require('./lib');
/* vim: set shiftwidth=2 tabstop=2 noexpandtab textwidth=80 wrap : */
"use strict";

try {
var Emitter = require('emitter');
} catch (e) {
Emitter = require('component-emitter');
}

module.exports = createModel;

function createModel(properties) {
function Model(data) {
if (!(this instanceof Model)) return new Model(data);
Emitter.call(this);
this._data = {};
data = data || {};
this._model.emit('construct', this, data);
for (var prop in data) {
if (!(prop in Model._skip))
this[prop] = data[prop];
}
}

new Emitter(Model);

Model._skip = {};
Model._attrs = {};
Model.attr = attr;
Model.use = use;

Model.prototype = Object.create(Emitter.prototype);
Model.prototype._model = Model;
Model.prototype._get = get;
Model.prototype._set = set;
Model.prototype.toJSON = toJSON;

properties = properties || [];
var i, prop;
if (Array.isArray(properties)) {
for (i = 0; i < properties.length; i++) {
prop = properties[i];
Model.attr(prop);
}
} else {
var keys = Object.keys(properties);
for (i = 0; i < keys.length; i++) {
prop = keys[i];
Model.attr(prop, properties[prop]);
}
}

return Model;
}

function use(fns) {
/*jshint validthis:true */
fns = Array.isArray(fns) ? fns : [fns];
for (var i = 0; i < fns.length; i++) {
var fn = fns[i];
fn(this);
}
return this;
}

function attr(name, metadata) {
/*jshint validthis:true */
this._attrs[name] = metadata || {};

Object.defineProperty(this.prototype, name, {
enumerable: true,
configurable: true,
get: function () { return this._get(name); },
set: function (val) { this._set(name, val); }
});

return this;
}

function get(name) {
/*jshint validthis:true */
return this._data[name];
}
function set(name, val) {
/*jshint validthis:true */
var prev = this._data[name];
this._data[name] = val;
this._model.emit('change', this, name, val, prev);
this._model.emit('change ' + name, this, val, prev);
this.emit('change', name, val, prev);
this.emit('change ' + name, val, prev);
}
// FIXME: this might be fragile?
var ignore = {
_data: true,
_callbacks: true // emitter
};
function toJSON() {
/*jshint validthis:true */
var json = {};
var keys = Object.keys(this);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (!(key in ignore))
json[key] = this[key];
}
for (var k in this._data)
json[k] = this._data[k];
return json;
}
103 changes: 0 additions & 103 deletions lib/index.js

This file was deleted.

8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -9,17 +9,17 @@
"Arpad Borsos <arpad.borsos@googlemail.com> (http://swatinem.de)"
],
"dependencies": {
"emitter-component": "*"
"component-emitter": "*"
},
"devDependencies": {
"istanbul": "*",
"mocha": "*",
"should": "*",
"jscoverage": "*",
"coveralls": "*",
"mocha-lcov-reporter": "*"
"jshint": "*"
},
"scripts": {
"test": "make test && make test-coveralls"
"test": "make test-coveralls"
},
"repository": "git://github.com/Swatinem/model.git",
"license": "LGPLv3"
Expand Down
20 changes: 10 additions & 10 deletions test/model.js
Expand Up @@ -39,13 +39,13 @@ describe('Model', function () {
var SomeModel = Model(['prop', 'prop2']);
var obj = new SomeModel();
var calls = 0;
obj.once('change', function (name, val, old) {
obj.once('change', function (name, val) {
calls++;
name.should.eql('prop');
val.should.eql(1);
});
obj.prop = 1;
obj.once('change', function (name, val, old) {
obj.once('change', function (name, val) {
calls++;
name.should.eql('prop2');
val.should.eql(2);
Expand All @@ -56,7 +56,7 @@ describe('Model', function () {
it('should allow passing an object of properties and metadata', function (done) {
var SomeModel = Model({prop: {some: 'metadata'}});
var obj = new SomeModel();
obj.once('change', function (name, val, old) {
obj.once('change', function (name, val) {
name.should.eql('prop');
val.should.eql(1);
done();
Expand All @@ -82,7 +82,7 @@ describe('Model', function () {
instance.should.be.an.instanceof(SomeModel);
done();
});
var obj = new SomeModel();
new SomeModel();
});
it('should delegate change events to the model', function (done) {
var SomeModel = Model().attr('prop');
Expand All @@ -104,12 +104,12 @@ describe('Model', function () {
});
it('should support constructing from a plain object', function (done) {
var SomeModel = Model().attr('prop');
SomeModel.once('change prop', function (instance, val, old) {
SomeModel.once('change prop', function (instance, val) {
instance.should.be.an.instanceof(SomeModel);
val.should.eql(1);
done();
});
var obj = new SomeModel({prop: 1});
new SomeModel({prop: 1});
});
describe('Extensibility', function () {
it('should make properties configurable', function () {
Expand All @@ -130,7 +130,7 @@ describe('Model', function () {
Model.prototype.save = function () {
this._data.should.eql({prop: 1});
done();
}
};
});
var obj = new SomeModel();
obj.prop = 1;
Expand All @@ -142,7 +142,7 @@ describe('Model', function () {
Model.validate = function (prop) {
prop.should.eql('prop');
done();
}
};
});
SomeModel.validate('prop');
});
Expand All @@ -160,12 +160,12 @@ describe('Model', function () {
this._dirty[name] = val;
name.should.eql('prop');
set.call(this, name, val);
}
};
Model.prototype._get = function (name) {
calls++;
this._dirty.should.eql({prop: 1});
return get.call(this, name);
}
};
});
var obj = new SomeModel();
obj.prop = 1;
Expand Down

0 comments on commit 153fe20

Please sign in to comment.