Skip to content

Commit

Permalink
Implement #propertyTypes() in terms of #propertyType().
Browse files Browse the repository at this point in the history
  • Loading branch information
Kit Cambridge committed Jul 9, 2013
1 parent 049d4c9 commit cf7606f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
21 changes: 18 additions & 3 deletions lib/polyclay.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ PolyClay.addType(
defaultFunc: function() { return {}; }
});

PolyClay.addType(
{
name: 'untyped',
validatorFunc: function() { return true; },
defaultFunc: function() { return null; }
});

PolyClay.Model.prototype.valid = function()
{
var i, p, len;
Expand Down Expand Up @@ -300,6 +307,7 @@ PolyClay.Model.addEnumerableProperty = function(obj, propname, enumerable)
PolyClay.Model.addOptionalProperty = function(obj, propname)
{
obj.prototype.__optional.push(propname);
obj.prototype.__types[propname] = 'untyped';

// No type validation on optional properties.
var result = function()
Expand Down Expand Up @@ -345,13 +353,20 @@ PolyClay.Model.prototype.serialize = function()

PolyClay.Model.prototype.propertyTypes = function()
{
var result = _.clone(this.__types);
for (var i = 0; i < this.__optional.length; i++)
result[this.__optional[i]] = 'untyped';
var props = this.__properties;
var result = {};

for (var i = 0; i < props.length; i++)
result[props[i]] = this.propertyType(props[i]);

return result;
};

PolyClay.Model.prototype.propertyType = function(prop)
{
return this.__types[prop];
};

PolyClay.Model.prototype.update = function(attr)
{
if (!_.isObject(attr))
Expand Down
13 changes: 11 additions & 2 deletions test/test-02-persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ describe('persistence layer', function()
assert.ok(Model.adapter instanceof MockDBAdapter);
});


it('emits before-save', function(done)
{
var obj = new Model();
Expand Down Expand Up @@ -338,7 +337,7 @@ describe('persistence layer', function()
});
});

it('propertyType() returns a hash of types for properties', function()
it('propertyTypes() returns a hash of types for properties', function()
{
var obj = new Model();

Expand All @@ -347,4 +346,14 @@ describe('persistence layer', function()

});

it('propertyType() can query the type of a specific property', function()
{
var obj = new Model();

obj.propertyType('key').should.equal('string');
obj.propertyType('required_prop').should.equal('string');
obj.propertyType('ephemeral').should.equal('untyped');
should.not.exist(obj.propertyType('nonexistent'));
});

});

0 comments on commit cf7606f

Please sign in to comment.