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
0 parents
commit 1b8d905
Showing
2 changed files
with
71 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
function def(ds){ | ||
var k = function () { this.init.apply(this, arguments); }; | ||
var sc = false; | ||
if(ds.extend) { | ||
sc = function() {}; | ||
sc.prototype = ds.extend.prototype; | ||
delete ds.extend; | ||
k.prototype = new sc(); | ||
} else { | ||
k.prototype = function(){}; | ||
} | ||
for(var p in ds){ | ||
if(sc && sc.prototype[p] && /\b_super\b/.test(ds[p])) { | ||
k.prototype[p] = (function(sfn, fn){ | ||
return function() { | ||
this._super = sfn; | ||
var res = fn.apply(this, arguments); | ||
delete this._super; | ||
return res; | ||
}; | ||
})(sc.prototype[p], ds[p]); | ||
} else { | ||
k.prototype[p] = ds[p]; | ||
} | ||
}; | ||
return k; | ||
} | ||
|
||
exports.def = def; |
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,42 @@ | ||
var def = require('./def').def; | ||
var fs = require('fs'); | ||
var kabin = exports; | ||
|
||
kabin.Raw = def({ | ||
init: function (params) { | ||
this.path = params.path | ||
var that = this; | ||
fs.readdirSync(this.path).forEach(function (dir) { that._defineAttributes(dir); }); | ||
}, | ||
|
||
_defineAttributes: function (id) { | ||
this.__defineGetter__(id, function () { | ||
return this._onGet(fs.readFileSync(this.path+"/"+id, "utf8")); | ||
}); | ||
this.__defineSetter__(id, function (value) { | ||
fs.writeFileSync(this.path+"/"+id, this._onSet(value)); | ||
}); | ||
}, | ||
|
||
insert: function (name, value) { | ||
this._defineAttributes(name); | ||
this[name] = value; | ||
}, | ||
|
||
_onSet: function(value) { return value; }, | ||
_onGet: function(value) { return value; }, | ||
}); | ||
|
||
kabin.JSON = def({ | ||
extend: kabin.Raw, | ||
init: function (params) { | ||
this._super(params); | ||
}, | ||
|
||
_onSet: function (value) { | ||
return JSON.stringify(value); | ||
}, | ||
_onGet: function (value) { | ||
return JSON.parse(value); | ||
} | ||
}); |