Comparing version 1.3.1 to 1.4.1
{ | ||
"name": "cla6", | ||
"description": "ES6 style class system", | ||
"version": "1.3.0", | ||
"version": "1.4.1", | ||
"main": "client/cla6.js", | ||
@@ -6,0 +6,0 @@ "repository": { |
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Cla6 = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var _ = require('./utils'); | ||
var Extender = require('./extender'); | ||
@@ -30,7 +31,22 @@ var ClassFactory = require('./classFactory'); | ||
if (plugin == null) | ||
throwErr('a plugin must be provided'); | ||
throwPluginErr('a plugin must be provided'); | ||
if (typeof plugin != 'function') | ||
throwErr('plugin must be a function'); | ||
if (typeof plugin != 'object') | ||
throwPluginErr('plugin must be an object'); | ||
if (plugin.manipulate == null) | ||
throwPluginErr('manipulator must be defined'); | ||
if (typeof plugin.manipulate != 'function') | ||
throwPluginErr('manipulator must be a function'); | ||
if (plugin.initialize != null && | ||
typeof plugin.initialize != 'function') | ||
throwPluginErr('initializer must be a function'); | ||
plugin = _.clone(plugin); | ||
if (plugin.initialize != null) | ||
plugin.initialize = plugin.initialize.bind(null, Cla6); | ||
PluginsManager.add(plugin); | ||
@@ -43,4 +59,8 @@ }; | ||
var throwPluginErr = function(msg) { | ||
throw Error('Cla6 plugin error - ' + msg); | ||
}; | ||
module.exports = Cla6; | ||
},{"./classFactory":2,"./extender":3,"./pluginsManager":5}],2:[function(require,module,exports){ | ||
},{"./classFactory":2,"./extender":3,"./pluginsManager":5,"./utils":6}],2:[function(require,module,exports){ | ||
var _ = require('./utils'); | ||
@@ -124,6 +144,6 @@ var ExtensionFactory = require('./extensionFactory'); | ||
if (props == null) | ||
throwErr('properties must be provided'); | ||
throwMixinErr('properties must be provided'); | ||
if (typeof props != 'object') | ||
throwErr('properties must be defined using an object'); | ||
throwMixinErr('properties must be defined using an object'); | ||
@@ -143,3 +163,3 @@ descriptors = _.toDescriptors(props); | ||
var throwErr = function(msg) { | ||
var throwMixinErr = function(msg) { | ||
throw Error('Cla6 mixin error - ' + msg); | ||
@@ -155,2 +175,5 @@ }; | ||
var add = function(plugin) { | ||
if (plugin.initialize != null) | ||
plugin.initialize(); | ||
plugins.push(plugin); | ||
@@ -166,3 +189,3 @@ }; | ||
plugins.forEach(function(plugin) { | ||
plugin(descriptors, Parent); | ||
plugin.manipulate(descriptors, Parent); | ||
}); | ||
@@ -178,12 +201,10 @@ }; | ||
var clone = function(obj) { | ||
return Object.keys(obj).reduce(function(result, k) { | ||
var descriptor = Object.getOwnPropertyDescriptor(obj, k); | ||
return Object.defineProperty(result, k, descriptor); | ||
}, {}); | ||
return extend({}, obj); | ||
}; | ||
var extend = function(obj, extension) { | ||
Object.keys(extension).forEach(function(k) { | ||
obj[k] = extension[k]; | ||
}); | ||
return Object.keys(extension).reduce(function(result, k) { | ||
var descriptor = Object.getOwnPropertyDescriptor(extension, k); | ||
return Object.defineProperty(result, k, descriptor); | ||
}, obj); | ||
}; | ||
@@ -190,0 +211,0 @@ |
@@ -1,1 +0,1 @@ | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Cla6=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var Extender=require("./extender");var ClassFactory=require("./classFactory");var PluginsManager=require("./pluginsManager");function Cla6(name,props){if(name==null)throwErr("a name must be provided");if(typeof name!="string")throwErr("name must be a string");if(props!=null){if(typeof props!="object")throwErr("properties must be defined using an object");if(props.hasOwnProperty("constructor")&&typeof props.constructor!="function")throwErr("constructor must be a function")}if(props==null)return new Extender(name);else return ClassFactory.create(name,props)}Cla6.use=function(plugin){if(plugin==null)throwErr("a plugin must be provided");if(typeof plugin!="function")throwErr("plugin must be a function");PluginsManager.add(plugin)};var throwErr=function(msg){throw Error("Cla6 error - "+msg)};module.exports=Cla6},{"./classFactory":2,"./extender":3,"./pluginsManager":5}],2:[function(require,module,exports){var _=require("./utils");var ExtensionFactory=require("./extensionFactory");var PluginsManager=require("./pluginsManager");var createClass=function(name,props,Parent){props=_.clone(props);if(typeof Parent!="function")Parent=Object;if(!props.hasOwnProperty("constructor"))props.constructor=function(){Parent.apply(this,arguments)};var descriptors=_.toDescriptors(props);PluginsManager.manipulate(descriptors,Parent);var Child=_.nameFn(descriptors.constructor.value,name);var extension=ExtensionFactory.create(Parent);_.extend(Child,extension);descriptors.constructor.value=Child;Child.prototype=Object.create(Parent.prototype,descriptors);return Child};module.exports={create:createClass}},{"./extensionFactory":4,"./pluginsManager":5,"./utils":6}],3:[function(require,module,exports){var ClassFactory=require("./classFactory");var Extender=ClassFactory.create("Extender",{constructor:function(name){this.name=name},extend:function(Parent,props){if(Parent==null)throwErr("a parent must be provided");if(typeof Parent!="function")throwErr("parent must be a function");if(props==null)throwErr("properties must be provided");if(typeof props!="object")throwErr("properties must be defined using an object");if(props.hasOwnProperty("constructor")&&typeof props.constructor!="function")throwErr("constructor must be a function");return ClassFactory.create(this.name,props,Parent)}});var throwErr=function(msg){throw Error("Cla6 extension error - "+msg)};module.exports=Extender},{"./classFactory":2}],4:[function(require,module,exports){var _=require("./utils");var PluginsManager=require("./pluginsManager");var createExtension=function(Parent){var mixin=function(props){if(arguments.length>1){Array.prototype.forEach.call(arguments,function(props){this.mixin(props)},this)}else{if(props==null)throwErr("properties must be provided");if(typeof props!="object")throwErr("properties must be defined using an object");descriptors=_.toDescriptors(props);PluginsManager.manipulate(descriptors,Parent);Object.defineProperties(this.prototype,descriptors)}return this};return{mixin:mixin}};var throwErr=function(msg){throw Error("Cla6 mixin error - "+msg)};module.exports={create:createExtension}},{"./pluginsManager":5,"./utils":6}],5:[function(require,module,exports){var plugins=[];var add=function(plugin){plugins.push(plugin)};var remove=function(plugin){var index=plugins.indexOf(plugin);plugins.splice(index,1)};var manipulate=function(descriptors,Parent){plugins.forEach(function(plugin){plugin(descriptors,Parent)})};module.exports={add:add,remove:remove,manipulate:manipulate}},{}],6:[function(require,module,exports){var clone=function(obj){return Object.keys(obj).reduce(function(result,k){var descriptor=Object.getOwnPropertyDescriptor(obj,k);return Object.defineProperty(result,k,descriptor)},{})};var extend=function(obj,extension){Object.keys(extension).forEach(function(k){obj[k]=extension[k]})};var nameFn=function(fn,name){return eval("(function "+name+"() {return fn.apply(this, arguments);})")};var toDescriptors=function(props){return Object.keys(props).reduce(function(result,k){var descriptor=Object.getOwnPropertyDescriptor(props,k);delete descriptor.enumerable;if(descriptor.value==null)delete descriptor.writable;result[k]=descriptor;return result},{})};module.exports={clone:clone,extend:extend,nameFn:nameFn,toDescriptors:toDescriptors}},{}]},{},[1])(1)}); | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Cla6=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var _=require("./utils");var Extender=require("./extender");var ClassFactory=require("./classFactory");var PluginsManager=require("./pluginsManager");function Cla6(name,props){if(name==null)throwErr("a name must be provided");if(typeof name!="string")throwErr("name must be a string");if(props!=null){if(typeof props!="object")throwErr("properties must be defined using an object");if(props.hasOwnProperty("constructor")&&typeof props.constructor!="function")throwErr("constructor must be a function")}if(props==null)return new Extender(name);else return ClassFactory.create(name,props)}Cla6.use=function(plugin){if(plugin==null)throwPluginErr("a plugin must be provided");if(typeof plugin!="object")throwPluginErr("plugin must be an object");if(plugin.manipulate==null)throwPluginErr("manipulator must be defined");if(typeof plugin.manipulate!="function")throwPluginErr("manipulator must be a function");if(plugin.initialize!=null&&typeof plugin.initialize!="function")throwPluginErr("initializer must be a function");plugin=_.clone(plugin);if(plugin.initialize!=null)plugin.initialize=plugin.initialize.bind(null,Cla6);PluginsManager.add(plugin)};var throwErr=function(msg){throw Error("Cla6 error - "+msg)};var throwPluginErr=function(msg){throw Error("Cla6 plugin error - "+msg)};module.exports=Cla6},{"./classFactory":2,"./extender":3,"./pluginsManager":5,"./utils":6}],2:[function(require,module,exports){var _=require("./utils");var ExtensionFactory=require("./extensionFactory");var PluginsManager=require("./pluginsManager");var createClass=function(name,props,Parent){props=_.clone(props);if(typeof Parent!="function")Parent=Object;if(!props.hasOwnProperty("constructor"))props.constructor=function(){Parent.apply(this,arguments)};var descriptors=_.toDescriptors(props);PluginsManager.manipulate(descriptors,Parent);var Child=_.nameFn(descriptors.constructor.value,name);var extension=ExtensionFactory.create(Parent);_.extend(Child,extension);descriptors.constructor.value=Child;Child.prototype=Object.create(Parent.prototype,descriptors);return Child};module.exports={create:createClass}},{"./extensionFactory":4,"./pluginsManager":5,"./utils":6}],3:[function(require,module,exports){var ClassFactory=require("./classFactory");var Extender=ClassFactory.create("Extender",{constructor:function(name){this.name=name},extend:function(Parent,props){if(Parent==null)throwErr("a parent must be provided");if(typeof Parent!="function")throwErr("parent must be a function");if(props==null)throwErr("properties must be provided");if(typeof props!="object")throwErr("properties must be defined using an object");if(props.hasOwnProperty("constructor")&&typeof props.constructor!="function")throwErr("constructor must be a function");return ClassFactory.create(this.name,props,Parent)}});var throwErr=function(msg){throw Error("Cla6 extension error - "+msg)};module.exports=Extender},{"./classFactory":2}],4:[function(require,module,exports){var _=require("./utils");var PluginsManager=require("./pluginsManager");var createExtension=function(Parent){var mixin=function(props){if(arguments.length>1){Array.prototype.forEach.call(arguments,function(props){this.mixin(props)},this)}else{if(props==null)throwMixinErr("properties must be provided");if(typeof props!="object")throwMixinErr("properties must be defined using an object");descriptors=_.toDescriptors(props);PluginsManager.manipulate(descriptors,Parent);Object.defineProperties(this.prototype,descriptors)}return this};return{mixin:mixin}};var throwMixinErr=function(msg){throw Error("Cla6 mixin error - "+msg)};module.exports={create:createExtension}},{"./pluginsManager":5,"./utils":6}],5:[function(require,module,exports){var plugins=[];var add=function(plugin){if(plugin.initialize!=null)plugin.initialize();plugins.push(plugin)};var remove=function(plugin){var index=plugins.indexOf(plugin);plugins.splice(index,1)};var manipulate=function(descriptors,Parent){plugins.forEach(function(plugin){plugin.manipulate(descriptors,Parent)})};module.exports={add:add,remove:remove,manipulate:manipulate}},{}],6:[function(require,module,exports){var clone=function(obj){return extend({},obj)};var extend=function(obj,extension){return Object.keys(extension).reduce(function(result,k){var descriptor=Object.getOwnPropertyDescriptor(extension,k);return Object.defineProperty(result,k,descriptor)},obj)};var nameFn=function(fn,name){return eval("(function "+name+"() {return fn.apply(this, arguments);})")};var toDescriptors=function(props){return Object.keys(props).reduce(function(result,k){var descriptor=Object.getOwnPropertyDescriptor(props,k);delete descriptor.enumerable;if(descriptor.value==null)delete descriptor.writable;result[k]=descriptor;return result},{})};module.exports={clone:clone,extend:extend,nameFn:nameFn,toDescriptors:toDescriptors}},{}]},{},[1])(1)}); |
@@ -0,1 +1,2 @@ | ||
var _ = require('./utils'); | ||
var Extender = require('./extender'); | ||
@@ -29,7 +30,22 @@ var ClassFactory = require('./classFactory'); | ||
if (plugin == null) | ||
throwErr('a plugin must be provided'); | ||
throwPluginErr('a plugin must be provided'); | ||
if (typeof plugin != 'function') | ||
throwErr('plugin must be a function'); | ||
if (typeof plugin != 'object') | ||
throwPluginErr('plugin must be an object'); | ||
if (plugin.manipulate == null) | ||
throwPluginErr('manipulator must be defined'); | ||
if (typeof plugin.manipulate != 'function') | ||
throwPluginErr('manipulator must be a function'); | ||
if (plugin.initialize != null && | ||
typeof plugin.initialize != 'function') | ||
throwPluginErr('initializer must be a function'); | ||
plugin = _.clone(plugin); | ||
if (plugin.initialize != null) | ||
plugin.initialize = plugin.initialize.bind(null, Cla6); | ||
PluginsManager.add(plugin); | ||
@@ -42,2 +58,6 @@ }; | ||
var throwPluginErr = function(msg) { | ||
throw Error('Cla6 plugin error - ' + msg); | ||
}; | ||
module.exports = Cla6; |
@@ -13,6 +13,6 @@ var _ = require('./utils'); | ||
if (props == null) | ||
throwErr('properties must be provided'); | ||
throwMixinErr('properties must be provided'); | ||
if (typeof props != 'object') | ||
throwErr('properties must be defined using an object'); | ||
throwMixinErr('properties must be defined using an object'); | ||
@@ -32,3 +32,3 @@ descriptors = _.toDescriptors(props); | ||
var throwErr = function(msg) { | ||
var throwMixinErr = function(msg) { | ||
throw Error('Cla6 mixin error - ' + msg); | ||
@@ -35,0 +35,0 @@ }; |
var plugins = []; | ||
var add = function(plugin) { | ||
if (plugin.initialize != null) | ||
plugin.initialize(); | ||
plugins.push(plugin); | ||
@@ -14,3 +17,3 @@ }; | ||
plugins.forEach(function(plugin) { | ||
plugin(descriptors, Parent); | ||
plugin.manipulate(descriptors, Parent); | ||
}); | ||
@@ -17,0 +20,0 @@ }; |
var clone = function(obj) { | ||
return Object.keys(obj).reduce(function(result, k) { | ||
var descriptor = Object.getOwnPropertyDescriptor(obj, k); | ||
return Object.defineProperty(result, k, descriptor); | ||
}, {}); | ||
return extend({}, obj); | ||
}; | ||
var extend = function(obj, extension) { | ||
Object.keys(extension).forEach(function(k) { | ||
obj[k] = extension[k]; | ||
}); | ||
return Object.keys(extension).reduce(function(result, k) { | ||
var descriptor = Object.getOwnPropertyDescriptor(extension, k); | ||
return Object.defineProperty(result, k, descriptor); | ||
}, obj); | ||
}; | ||
@@ -13,0 +11,0 @@ |
{ | ||
"name": "cla6", | ||
"description": "ES6 style class system", | ||
"version": "1.3.1", | ||
"version": "1.4.1", | ||
"main": "lib/cla6.js", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -133,5 +133,5 @@ # Cla6.js | ||
A Cla6 plugin is a manupulation function which gets the classe's descriptors and parent anytime before it gets created, thus the properties can be manipulated. Multipile plugins can be applied and will be called by their use order. | ||
A plugin is a module which adds functionality to Cla6 and can be loaded dynamcally. Multipile plugins can be applied and will be called by their order of use. | ||
Note, each plugin will affect the descriptors object for the next plugin in the plugins chain. | ||
Note, each plugin will affect the arguments for the next plugin in the plugins chain. | ||
@@ -147,2 +147,24 @@ ```js | ||
### Building a plugin | ||
A basic plugin stracture should look like so: | ||
```js | ||
var initialize = function(Cla6) { | ||
// initializer logic | ||
}; | ||
var manipulate = function(descriptors, Parent) { | ||
// manipulator logic | ||
}; | ||
module.exports = { | ||
initialize: initialize, | ||
manipulate: manipulate | ||
}; | ||
``` | ||
- `initialize` - An optional initializer function which will be called with Cla6 instance once the plugin is used. | ||
- `manipulate` - A required manipulator function which will be called every time before a class gets created with its descriptors and Parent class. | ||
## Download | ||
@@ -149,0 +171,0 @@ |
@@ -69,3 +69,3 @@ var Chai = require('chai'); | ||
var plugin = spy(function(descriptors) { | ||
var manipulate = spy(function(descriptors) { | ||
expect(descriptors).to.have.all.keys('method', 'accessor'); | ||
@@ -79,9 +79,13 @@ expect(descriptors.method.value).to.equal(mixin.method); | ||
var plugin = { | ||
manipulate: manipulate | ||
}; | ||
Cla6.use(plugin); | ||
this.Klass.mixin(mixin); | ||
expect(plugin).to.have.been.called.once; | ||
expect(plugin.manipulate).to.have.been.called.once; | ||
unuse(plugin); | ||
this.Klass.mixin(mixin); | ||
expect(plugin).to.have.been.called.once; | ||
expect(plugin.manipulate).to.have.been.called.once; | ||
}); | ||
@@ -88,0 +92,0 @@ }); |
@@ -17,9 +17,35 @@ var Chai = require('chai'); | ||
it('should throw an error if plugin is not a function', function() { | ||
it('should throw an error if plugin is not an object', function() { | ||
bondUse = Cla6.use.bind(null, false); | ||
expect(bondUse).to.throw(Error, /plugin.*function/); | ||
expect(bondUse).to.throw(Error, /plugin.*object/); | ||
}); | ||
it('should throw an error if manupulator is not defined', function() { | ||
var plugin = {}; | ||
bondUse = Cla6.use.bind(null, plugin); | ||
expect(bondUse).to.throw(Error, /manipulator/); | ||
}); | ||
it('should throw an error if manupulator is not a function', function() { | ||
var plugin = { | ||
manipulate: false | ||
}; | ||
bondUse = Cla6.use.bind(null, plugin); | ||
expect(bondUse).to.throw(Error, /manipulator.*function/); | ||
}); | ||
it('should throw an error if initializer is not a function', function() { | ||
var plugin = { | ||
manipulate: function() {}, | ||
initialize: false | ||
}; | ||
bondUse = Cla6.use.bind(null, plugin); | ||
expect(bondUse).to.throw(Error, /initializer.*function/); | ||
}); | ||
}); | ||
it('should call plugin with class\'es descriptors and parent', function() { | ||
it('should call manipulator with class\'es descriptors and parent', function() { | ||
var props = { | ||
@@ -31,3 +57,3 @@ method: function() {}, | ||
var plugin = spy(function(descriptors, Parent) { | ||
var manipulate = spy(function(descriptors, Parent) { | ||
expect(descriptors).to.have.all.keys('constructor', 'method', 'accessor'); | ||
@@ -43,11 +69,30 @@ expect(descriptors.method.value).to.equal(props.method); | ||
var plugin = { | ||
manipulate: manipulate | ||
}; | ||
Cla6.use(plugin); | ||
Cla6('Klass').extend(Array, props); | ||
expect(plugin).to.have.been.called.once; | ||
expect(plugin.manipulate).to.have.been.called.once; | ||
unuse(plugin); | ||
Cla6('Klass', props); | ||
expect(plugin).to.have.been.called.once; | ||
expect(plugin.manipulate).to.have.been.called.once; | ||
}); | ||
}); | ||
it('should call initializer with Cla6', function() { | ||
var initialize = spy(function(module) { | ||
expect(module).to.equal(Cla6); | ||
}); | ||
var plugin = { | ||
manipulate: function() {}, | ||
initialize: initialize | ||
} | ||
Cla6.use(plugin); | ||
expect(plugin.initialize).to.have.been.called.once; | ||
unuse(plugin); | ||
}); | ||
}); |
33331
666
174