Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
156 additions
and
139 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
node_modules | ||
components | ||
lib-cov | ||
coverage.html | ||
coverage | ||
npm-debug.log | ||
*.tgz | ||
*.patch | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
node_modules | ||
components | ||
lib-cov | ||
coverage.html | ||
coverage | ||
npm-debug.log | ||
*.tgz | ||
*.patch | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file was deleted.
Oops, something went wrong.
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
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