classified-magic
Advanced tools
Comparing version 0.0.3 to 0.0.5
400
magic.js
@@ -1,225 +0,227 @@ | ||
(function() { | ||
var classified = require('classified'); | ||
var separator = require('path').sep; | ||
var classified = require('classified'); | ||
var separator = require('path').sep; | ||
if(typeof Proxy === 'undefined') { | ||
var Proxy = require('node-proxy'); | ||
} | ||
/* Definition | ||
-------------------------------*/ | ||
var magic = function() { | ||
var method = function() { | ||
return method.load.apply(method, arguments); | ||
}, | ||
if(typeof Proxy === 'undefined') { | ||
var Proxy = require('node-proxy'); | ||
} | ||
singleton = false, | ||
/* Definition | ||
instance = null, | ||
definition = classified(); | ||
/** | ||
* Defines the class | ||
* | ||
* @param function|object - if function, must return an object | ||
* @return this | ||
*/ | ||
method.define = function(prototype) { | ||
definition.define(prototype); | ||
return this; | ||
}; | ||
/** | ||
* Returns the full class definition | ||
* including the protected and private | ||
* properties | ||
* | ||
* @return object | ||
*/ | ||
method.definition = function() { | ||
return definition.definition(); | ||
}; | ||
/** | ||
* Returns all the parents of this definition | ||
* | ||
* @return array | ||
*/ | ||
method.parents = function() { | ||
return definition.parents(); | ||
}; | ||
/** | ||
* Adds a parent to be combined with the definition | ||
* | ||
* @param function|object - if function, will use prototype | ||
* @return this | ||
*/ | ||
method.extend = function(prototype) { | ||
//if prototype is a string | ||
if(typeof prototype === 'string') { | ||
//its a path to a file | ||
var path = _getCallerPath(); | ||
//if we have a path | ||
if(path) { | ||
//require it | ||
prototype = require((path + separator + prototype) | ||
.replace(separator + separator, separator)); | ||
if(typeof prototype.definition === 'function') { | ||
prototype = prototype.definition(); | ||
} | ||
} | ||
} | ||
definition.extend(prototype); | ||
return this; | ||
}; | ||
/** | ||
* Returns the publically accessable | ||
* class definition function | ||
* | ||
* @return function | ||
*/ | ||
method.get = function() { | ||
return definition.get(); | ||
}; | ||
/** | ||
* Returns class defined instantiation | ||
* | ||
* @return object | ||
*/ | ||
method.load = function() { | ||
//if no instance or no singleton | ||
if(!instance || !singleton) { | ||
instance = _makeItMagic(this.get().load.apply(null, arguments)); | ||
} | ||
return instance; | ||
}; | ||
/** | ||
* Sets loader to return a single instance | ||
* | ||
* @param bool | ||
* @return this | ||
*/ | ||
method.singleton = function(yes) { | ||
singleton = yes !== false; | ||
return this; | ||
}; | ||
/* Private Methods | ||
-------------------------------*/ | ||
var magic = function() { | ||
var method = {}, definition = classified(); | ||
var _getCallerPath = function() { | ||
var prepareStackTrace = Error.prepareStackTrace; | ||
Error.prepareStackTrace = function (_, stack) { | ||
return stack; | ||
} | ||
// Create a new `Error`, which automatically gets `stack` | ||
var error = new Error(); | ||
// Evaluate `err.stack`, which calls our new `Error.prepareStackTrace` | ||
var stack = error.stack; | ||
// Restore original `Error.prepareStackTrace` | ||
Error.prepareStackTrace = prepareStackTrace; | ||
/** | ||
* Defines the class | ||
* | ||
* @param function|object - if function, must return an object | ||
* @return this | ||
*/ | ||
method.define = function(prototype) { | ||
definition.define(prototype); | ||
return this; | ||
}; | ||
if(typeof stack[3] !== 'object' | ||
|| typeof stack[3].receiver !== 'object' | ||
|| typeof stack[3].receiver.filename !== 'string' ) { | ||
return false; | ||
} | ||
/** | ||
* Returns the full class definition | ||
* including the protected and private | ||
* properties | ||
* | ||
* @return object | ||
*/ | ||
method.definition = function() { | ||
return definition.definition(); | ||
}; | ||
var pathArray = stack[3].receiver.filename.split('/'); | ||
/** | ||
* Returns all the parents of this definition | ||
* | ||
* @return array | ||
*/ | ||
method.parents = function() { | ||
return definition.parents(); | ||
}; | ||
pathArray.pop(); | ||
/** | ||
* Adds a parent to be combined with the definition | ||
* | ||
* @param function|object - if function, will use prototype | ||
* @return this | ||
*/ | ||
method.extend = function(prototype) { | ||
//if prototype is a string | ||
if(typeof prototype === 'string') { | ||
//its a path to a file | ||
var path = _getCallerPath(); | ||
//if we have a path | ||
if(path) { | ||
//require it | ||
prototype = require((path + separator + prototype) | ||
.replace(separator + separator, separator)); | ||
} | ||
return pathArray.join('/'); | ||
}; | ||
var _isMagicBinded = function() { | ||
return arguments.callee.caller.caller.toString().indexOf('__classifiedBinded__') !== -1 | ||
}; | ||
var _makeItMagic = function(instance) { | ||
//if no magic :( | ||
if(typeof instance.___get !== 'function' | ||
&& typeof instance.___set !== 'function' | ||
&& typeof instance.___enum !== 'function' | ||
&& typeof instance.___has !== 'function' | ||
&& typeof instance.___delete !== 'function' | ||
&& typeof instance.___fix !== 'function') { | ||
return instance; | ||
} | ||
var magic = {}; | ||
magic.get = function(receiver, name) { | ||
//if it exists | ||
if(typeof instance[name] !== 'undefined' || _isMagicBinded()) { | ||
return instance[name]; | ||
} | ||
definition.extend(prototype); | ||
return this; | ||
if(typeof instance.___get === 'function') { | ||
return instance.___get.call(instance, name); | ||
} | ||
}; | ||
/** | ||
* Returns the publically accessable | ||
* class definition function | ||
* | ||
* @return function | ||
*/ | ||
method.get = function() { | ||
return definition.get(); | ||
}; | ||
/** | ||
* Returns class defined instantiation | ||
* | ||
* @return object | ||
*/ | ||
method.load = function() { | ||
return _makeItMagic(this.get().load.apply(null, arguments)); | ||
}; | ||
/* Private Methods | ||
-------------------------------*/ | ||
var _getCallerPath = function() { | ||
var prepareStackTrace = Error.prepareStackTrace; | ||
Error.prepareStackTrace = function (_, stack) { | ||
return stack; | ||
magic.set = function(receiver, name, value) { | ||
//if it exists | ||
if(typeof instance[name] !== 'undefined' || _isMagicBinded()) { | ||
instance[name] = value; | ||
return; | ||
} | ||
// Create a new `Error`, which automatically gets `stack` | ||
var error = new Error(); | ||
// Evaluate `err.stack`, which calls our new `Error.prepareStackTrace` | ||
var stack = error.stack; | ||
// Restore original `Error.prepareStackTrace` | ||
Error.prepareStackTrace = prepareStackTrace; | ||
if(typeof stack[3] !== 'object' | ||
|| typeof stack[3].receiver !== 'object' | ||
|| typeof stack[3].receiver.filename !== 'string' ) { | ||
return false; | ||
if(typeof instance.___set === 'function') { | ||
instance.___set.call(instance, name, value); | ||
return; | ||
} | ||
var pathArray = stack[3].receiver.filename.split('/'); | ||
instance[name] = value; | ||
}; | ||
magic.enumerate = function() { | ||
//if enum is set | ||
if(typeof instance.___enum === 'function') { | ||
return instance.___enum.call(instance); | ||
} | ||
pathArray.pop(); | ||
return pathArray.join('/'); | ||
return Object.keys(instance); | ||
}; | ||
var _isMagicBinded = function() { | ||
return arguments.callee.caller.caller.toString().indexOf('__classifiedBinded__') !== -1 | ||
magic.has = function(name) { | ||
//if enum is set | ||
if(typeof instance.___has === 'function') { | ||
return instance.___has.call(instance, name); | ||
} | ||
return Object.keys(instance); | ||
}; | ||
var _makeItMagic = function(instance) { | ||
//if no magic :( | ||
if(typeof instance.___get !== 'function' | ||
&& typeof instance.___set !== 'function' | ||
&& typeof instance.___enum !== 'function' | ||
&& typeof instance.___has !== 'function' | ||
&& typeof instance.___delete !== 'function' | ||
&& typeof instance.___fix !== 'function') { | ||
return instance; | ||
magic.delete = function(name) { | ||
//if enum is set | ||
if(!_isMagicBinded() && typeof instance.___delete === 'function') { | ||
return instance.___delete.call(instance, name); | ||
} | ||
var magic = {}; | ||
magic.get = function(receiver, name) { | ||
//if it exists | ||
if(typeof instance[name] !== 'undefined' || _isMagicBinded()) { | ||
return instance[name]; | ||
} | ||
if(typeof instance.___get === 'function') { | ||
return instance.___get.call(instance, name); | ||
} | ||
}; | ||
magic.set = function(receiver, name, value) { | ||
//if it exists | ||
if(typeof instance[name] !== 'undefined' || _isMagicBinded()) { | ||
instance[name] = value; | ||
return; | ||
} | ||
if(typeof instance.___set === 'function') { | ||
instance.___set.call(instance, name, value); | ||
return; | ||
} | ||
instance[name] = value; | ||
}; | ||
magic.enumerate = function() { | ||
//if enum is set | ||
if(typeof instance.___enum === 'function') { | ||
return instance.___enum.call(instance); | ||
} | ||
return Object.keys(instance); | ||
}; | ||
magic.has = function(name) { | ||
//if enum is set | ||
if(typeof instance.___has === 'function') { | ||
return instance.___has.call(instance, name); | ||
} | ||
return Object.keys(instance); | ||
}; | ||
magic.delete = function(name) { | ||
//if enum is set | ||
if(!_isMagicBinded() && typeof instance.___delete === 'function') { | ||
return instance.___delete.call(instance, name); | ||
} | ||
delete instance[name]; | ||
}; | ||
return Proxy.create(magic, instance); | ||
delete instance[name]; | ||
}; | ||
return method; | ||
return Proxy.create(magic, instance); | ||
}; | ||
/* Adaptor | ||
-------------------------------*/ | ||
//if node | ||
if(typeof module === 'object' && module.exports) { | ||
module.exports = function(definition) { | ||
definition = definition || {}; | ||
return magic().define(definition); | ||
}; | ||
//if AMD | ||
} else if(typeof define === 'function') { | ||
define(function() { | ||
return function(definition) { | ||
definition = definition || {}; | ||
return magic().define(definition); | ||
}; | ||
}); | ||
//how about jQuery? | ||
} else if(typeof jQuery === 'function' && typeof jQuery.extend === 'function') { | ||
jQuery.extend({ | ||
magic: function(definition) { | ||
definition = definition || {}; | ||
return magic().define(definition); | ||
} | ||
}); | ||
} else if(typeof window === 'object') { | ||
window.magic = function(definition) { | ||
definition = definition || {}; | ||
return magic().define(definition); | ||
} | ||
} | ||
})(); | ||
return method; | ||
}; | ||
/* Adaptor | ||
-------------------------------*/ | ||
module.exports = function(definition) { | ||
definition = definition || {}; | ||
return magic().define(definition); | ||
}; |
{ | ||
"name": "classified-magic", | ||
"description": "OOP for NodeJS with magic", | ||
"version": "0.0.3", | ||
"version": "0.0.5", | ||
"author": { | ||
@@ -21,3 +21,3 @@ "name": "Christian Blanquera", | ||
"dependencies": { | ||
"classified": "0.0.9" , | ||
"classified": "0.0.10" , | ||
"node-proxy": "0.8.0" | ||
@@ -24,0 +24,0 @@ }, |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
15831
382
+ Addedclassified@0.0.10(transitive)
- Removedclassified@0.0.9(transitive)
Updatedclassified@0.0.10