Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tnlogy committed Oct 12, 2011
0 parents commit 1b8d905
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
29 changes: 29 additions & 0 deletions def.js
@@ -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;
42 changes: 42 additions & 0 deletions kabin.js
@@ -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);
}
});

0 comments on commit 1b8d905

Please sign in to comment.