Skip to content

Instantly share code, notes, and snippets.

@llambeau
Created April 28, 2014 15:10
Show Gist options
  • Save llambeau/11374984 to your computer and use it in GitHub Desktop.
Save llambeau/11374984 to your computer and use it in GitHub Desktop.
var Finitio = require('finitio.js');
var TypeFactory = require('finitio.js/lib/support/factory');
var _ = require('underscore');
var $ = function(name, obj){
var source = obj.toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
var result = Finitio.parse(source);
return result;
};
var User = $('User', function(){/*
UUID = .String( s | s.match(new RegExp("^[a-zA-Z0-9]{17}$")) )
{
id : UUID,
name : .String,
lastname :? .String,
birthdate : .Date
}
*/});
function toObject(system){
var fields = system.main.heading.attributes;
var obj = {};
Object.defineProperty(obj, '__system', {
value: system,
enumerable: false
});
// Create the properties on the object
_.each(fields, function(attr){
var value = null;
var name = attr.name;
var type = attr.type;
Object.defineProperty(obj, name, {
enumerable: true,
get: function(){ return value; },
set: function(newval){
console.log(value, '->', newval);
value = type.dress(newval);
}
});
});
return obj;
}
var user = toObject(User);
user.name = 'Louis';
user.birthdate = new Date().getTime();
user.id = '3' // will throw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment