custom-ability
Advanced tools
Comparing version 1.1.1 to 1.2.0
(function() { | ||
"use strict"; | ||
var extend, extendFilter, injectMethods, isArray, | ||
'use strict'; | ||
var defineProperty, extend, extendFilter, injectMethods, isArray, isFunction, | ||
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | ||
isArray = require("util-ex/lib/is/type/array"); | ||
isArray = require('util-ex/lib/is/type/array'); | ||
extend = require("util-ex/lib/_extend"); | ||
isFunction = require('util-ex/lib/is/type/function'); | ||
extendFilter = require("util-ex/lib/extend"); | ||
extend = require('util-ex/lib/_extend'); | ||
injectMethods = require("util-ex/lib/injectMethods"); | ||
extendFilter = require('util-ex/lib/extend'); | ||
injectMethods = require('util-ex/lib/injectMethods'); | ||
defineProperty = require('util-ex/lib/defineProperty'); | ||
module.exports = function(abilityClass, aCoreMethod, isGetClassFunc) { | ||
return function(aClass, aOptions) { | ||
var AbilityClass, filter, vExcludes, vIncludes; | ||
var abilityFn; | ||
abilityFn = function(aClass, aOptions) { | ||
var AbilityClass, filter, filterMethods, vAbility, vExcludes, vIncludes, vName; | ||
AbilityClass = abilityClass; | ||
@@ -21,5 +26,25 @@ if (isGetClassFunc === true) { | ||
} | ||
if (!AbilityClass) { | ||
throw new TypeError('no abilityClass'); | ||
} | ||
if (aClass == null) { | ||
aClass = AbilityClass; | ||
} else if (!aClass.prototype[aCoreMethod]) { | ||
} else if (!(aClass.prototype.$abilities && aClass.prototype.$abilities.self)) { | ||
if (!(aOptions && aOptions.inited) && (vName = AbilityClass.name) && aClass.prototype.$abilities && (vAbility = aClass.prototype.$abilities[vName.toLowerCase()])) { | ||
if (aOptions) { | ||
aOptions.inited = true; | ||
} else { | ||
aOptions = { | ||
inited: true | ||
}; | ||
} | ||
return vAbility(aClass, aOptions); | ||
} | ||
if (!aClass.prototype.$abilities) { | ||
defineProperty(aClass.prototype, '$abilities', { | ||
self: abilityFn | ||
}); | ||
} else { | ||
aClass.prototype.$abilities.self = abilityFn; | ||
} | ||
if ((aOptions == null) || !(aOptions.include || aOptions.exclude)) { | ||
@@ -37,4 +62,8 @@ extend(aClass, AbilityClass); | ||
} | ||
if (aOptions.includeAlways !== false) { | ||
vIncludes.push(aCoreMethod); | ||
if (aCoreMethod) { | ||
if (isArray(aCoreMethod)) { | ||
vIncludes = vIncludes.concat(aCoreMethod); | ||
} else { | ||
vIncludes.push(aCoreMethod); | ||
} | ||
} | ||
@@ -64,4 +93,16 @@ vExcludes = aOptions.exclude; | ||
}; | ||
filterMethods = function(methods) { | ||
var k; | ||
if (methods instanceof Object) { | ||
for (k in methods) { | ||
if (!filter(k)) { | ||
delete methods[k]; | ||
} | ||
} | ||
} | ||
}; | ||
extendFilter(aClass, AbilityClass, filter); | ||
extendFilter(aClass.prototype, AbilityClass.prototype, filter); | ||
filterMethods(aOptions.methods); | ||
filterMethods(aOptions.classMethods); | ||
} | ||
@@ -79,2 +120,3 @@ if (aOptions != null) { | ||
}; | ||
return abilityFn; | ||
}; | ||
@@ -81,0 +123,0 @@ |
{ | ||
"name": "custom-ability", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "make custom ability more easy. generate the ability which can be added to any class directly.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/snowyu/custom-ability.js", |
@@ -15,3 +15,3 @@ ### custom-ability [![Build Status](https://img.shields.io/travis/snowyu/custom-ability.js/master.png)](http://travis-ci.org/snowyu/custom-ability.js) [![npm](https://img.shields.io/npm/v/custom-ability.svg)](https://npmjs.org/package/custom-ability) [![downloads](https://img.shields.io/npm/dm/custom-ability.svg)](https://npmjs.org/package/custom-ability) [![license](https://img.shields.io/npm/l/custom-ability.svg)](https://npmjs.org/package/custom-ability) | ||
* customAbility(abilityClass, coreMethod[, isGetClassFunction]) | ||
* customAbility(abilityClass[, coreMethod[, isGetClassFunction]]) | ||
@@ -21,4 +21,3 @@ __arguments__ | ||
* abilityClass *(function)*: the class will become to ability able. | ||
* coreMethod *(string)*: the core instance method name to identify this ability. | ||
* it's the key to avoid duplication injection. | ||
* coreMethod *(string|array)*: optional must have coreMethod(s). | ||
* isGetClassFunction *(boolean)*: the `AbilityClass` is a `function(aClass, aOptions)` | ||
@@ -118,1 +117,49 @@ to return the real `Ability Class` if true. defaults to false. | ||
More complicated example, you can see the [events-ex/src/eventable.coffee](https://github.com/snowyu/events-ex.js). | ||
## additional $abilities | ||
In order to make certain ability to work, you need to modify some methods | ||
of the class. this time we need the "additional abilities" now. eg, the | ||
event-able ability to [AbstractObject](https://github.com/snowyu/abstract-object). | ||
We need to send a notification event when the state of the object changes. | ||
So the event-able of [AbstractObject](https://github.com/snowyu/abstract-object) | ||
should be: | ||
```coffee | ||
eventable = require 'events-ex/eventable' | ||
module.exports = (aClass)-> | ||
eventable aClass, methods: | ||
# override methods: | ||
setObjectState: (value, emitted = true)-> | ||
self= @self | ||
@super.call(self, value) | ||
self.emit value, self if emitted | ||
return | ||
# more detail on [AbstractObject/src/eventable](https://github.com/snowyu/abstract-object) | ||
``` | ||
the original `eventable('events-ex/eventable')` is no useful for AbstractObject. | ||
but we wanna the original `eventable('events-ex/eventable')` knows the changes | ||
and use it automatically. | ||
```coffee | ||
eventable = require 'events-ex/eventable' | ||
class MyClass | ||
inherits MyClass, AbstractObject | ||
eventable MyClass | ||
``` | ||
you just do this on the AbstractObject: | ||
```coffee | ||
AbstractObject = require('./lib/abstract-object') | ||
AbstractObject.$abilities = | ||
eventable: require('./eventable') | ||
module.exports = AbstractObject | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25118
14
127
163
1