classified
Advanced tools
Comparing version 0.0.11 to 0.0.12
@@ -16,7 +16,7 @@ /** | ||
-------------------------------*/ | ||
var classified = function() { | ||
var registry = {}, classified = function() { | ||
/* Constants | ||
-------------------------------*/ | ||
var INVALID_DEFINE = 'Expecting argument 1 in define() to be an object or return an object.'; | ||
var INVALID_EXTEND = 'Expecting argument 1 in extend() to be an object or return an object.'; | ||
var INVALID_TRAIT = 'Expecting argument 1 in trait() to be a registry item, an object or return an object.'; | ||
@@ -30,3 +30,2 @@ /* Properties | ||
definition = {}, | ||
extend = []; | ||
@@ -104,3 +103,16 @@ | ||
*/ | ||
method.extend = function(prototype) { | ||
method.trait = function(prototype) { | ||
//if prototype is a string | ||
//and it's definied in the registry | ||
if(typeof prototype === 'string') { | ||
//is it a saved state? | ||
if(typeof registry[prototype] !== 'undefined') { | ||
prototype = registry[prototype]; | ||
//its a string and we do not know | ||
//what to do with it | ||
} else { | ||
throw INVALID_TRAIT; | ||
} | ||
} | ||
//if prototype is a function | ||
@@ -114,3 +126,3 @@ if(typeof prototype === 'function') { | ||
if(typeof prototype !== 'object') { | ||
throw INVALID_EXTEND; | ||
throw INVALID_TRAIT; | ||
} | ||
@@ -124,2 +136,12 @@ | ||
/** | ||
* Creates a child definition | ||
* | ||
* @param function|object - if function, must return object | ||
* @return function | ||
*/ | ||
method.extend = function(prototype) { | ||
return classified().define(prototype).trait(this.definition()); | ||
}; | ||
/** | ||
* Returns the publically accessable | ||
@@ -131,3 +153,3 @@ * class definition function | ||
method.get = function() { | ||
var stack = { method: 0, parent: 0 }, | ||
var stack = { method: 0, parents: 0 }, | ||
final = {}, | ||
@@ -194,2 +216,13 @@ parents = {}, | ||
}; | ||
/** | ||
* Registers this class for extend | ||
* | ||
* @param string | ||
* @return this | ||
*/ | ||
method.register = function(name) { | ||
registry[name] = this.definition(); | ||
return this; | ||
}; | ||
@@ -309,3 +342,3 @@ /* Class Container | ||
//for parents add | ||
if(!stack.parent++) { | ||
if(!stack.parents++) { | ||
//lets set up private | ||
@@ -322,3 +355,3 @@ for(property in secret) { | ||
//if there is no more stack count | ||
if(!--stack.parent) { | ||
if(!--stack.parents) { | ||
//remove private | ||
@@ -501,2 +534,3 @@ for(property in secret) { | ||
}); | ||
//ok fine lets put it in windows. | ||
} else if(typeof window === 'object') { | ||
@@ -503,0 +537,0 @@ window.classified = function(definition) { |
{ | ||
"name": "classified", | ||
"description": "Real OOP for Javascript", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Christian Blanquera", |
@@ -76,3 +76,3 @@ #Classified | ||
//you can use object or function as the definition | ||
var ChildClass = classified(function() { | ||
var ChildClass = RootClass.extend(function() { | ||
var prototype = {}; | ||
@@ -97,3 +97,3 @@ | ||
return prototype; | ||
}).extend(RootClass.definition()); | ||
}); | ||
@@ -118,2 +118,3 @@ //instantiate child | ||
* Inheritance | ||
* Traits | ||
* Works on server or client | ||
@@ -135,2 +136,4 @@ | ||
* load() - Returns class defined instantiation | ||
* register(string) - saves a state of the definition which can be recalled in trait | ||
* trait(string|function|object) - will setup the provided as a parent, if string will recall from registry | ||
@@ -137,0 +140,0 @@ ### What's up with the underscores? |
@@ -120,3 +120,3 @@ var assert = require('assert'); | ||
it('should copy constants', function() { | ||
var child = classified({ SOME_CONSTANT_2: 44.5 }).extend(Root.definition()).load(); | ||
var child = classified({ SOME_CONSTANT_2: 44.5 }).trait(Root.definition()).load(); | ||
assert.equal('foo', child.SOME_CONSTANT); | ||
@@ -126,3 +126,3 @@ }); | ||
it('should call construct', function() { | ||
var child = classified({}).extend(Root.definition()).load(); | ||
var child = classified({}).trait(Root.definition()).load(); | ||
assert.equal(true, child.constructCalled); | ||
@@ -132,3 +132,3 @@ }); | ||
it('should be the same as root', function() { | ||
var child = classified({}).extend(Root.definition()).load(); | ||
var child = classified({}).trait(Root.definition()).load(); | ||
assert.equal(child.sampleProperty, root.sampleProperty); | ||
@@ -144,3 +144,3 @@ assert.equal(child.sampleDeepProperty.sample1, root.sampleDeepProperty.sample1); | ||
it('should not change properties of root', function() { | ||
var child = classified({}).extend(Root.definition()).load(); | ||
var child = classified({}).trait(Root.definition()).load(); | ||
@@ -157,5 +157,5 @@ child.sampleProperty = 5.5; | ||
it('should be able to add properties', function() { | ||
var child = classified({ | ||
var child = Root.extend({ | ||
childSample: 4 | ||
}).extend(Root.definition()).load(); | ||
}).load(); | ||
@@ -175,3 +175,3 @@ child.sampleProperty = 5.5; | ||
} | ||
}).extend(Root.definition()).load(); | ||
}).trait(Root.definition()).load(); | ||
@@ -186,3 +186,3 @@ assert.equal('foo', child.sampleMethod()); | ||
} | ||
}).extend(Root.definition()).load(); | ||
}).trait(Root.definition()).load(); | ||
@@ -197,14 +197,26 @@ assert.equal('_bar', child.sampleMethod()); | ||
} | ||
}).extend(Root.definition()).load(); | ||
}).trait(Root.definition()).load(); | ||
assert.equal('undefined', child.sampleMethod()); | ||
}); | ||
it('should not be able to register and access root', function() { | ||
Root.register('rooty'); | ||
var child = classified({ | ||
sampleMethod: function() { | ||
return typeof this.___parent.__sampleMethod; | ||
} | ||
}).trait('rooty').load(); | ||
assert.equal('undefined', child.sampleMethod()); | ||
}); | ||
}); | ||
describe('Grand Children Tests', function() { | ||
var Child = classified({ | ||
var Child = Root.extend({ | ||
sampleMethod: function() { | ||
return this.___parent._sampleMethod(); | ||
} | ||
}).extend(Root.definition()); | ||
}); | ||
@@ -217,3 +229,3 @@ var Trait = classified({ | ||
var grand = classified({ | ||
var grand = Child.extend({ | ||
sampleMethod: function() { | ||
@@ -226,3 +238,3 @@ return this.___parent.sampleMethod(); | ||
} | ||
}).extend(Child.definition()).extend(Trait.definition()).load(); | ||
}).trait(Trait.definition()).load(); | ||
@@ -229,0 +241,0 @@ it('should be able access parent protected methods', function() { |
24029
5
652
140