Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
cocktail-trait-configurable
Advanced tools
Provides configurable method to set properties on host class by a given object
A trait to add configure(options)
method that will call setters on each options key.
npm install cocktail --save
npm install cocktail-trait-configurable --save
None
MyClass.js
var cocktail = require('cocktail'),
Configurable = require('cocktail-trait-configurable');
cocktail.mix({
'@exports': module,
'@as' : 'class',
'@traits' : [Configurable],
'@properties': {
property1: 'default value',
property2: 0
},
constructor: function(config) {
//we can use configurable method in the constructor
this.configure(config);
}
});
index.js
var MyClass = require('./MyClass'),
options, obj, obj2;
options = {
property1: 'value from options',
property2: false,
discarded: 'this should be discarded!'
});
obj = new MyClass();
//use the configure method publicly
obj.configure(options);
console.log(obj.getProperty1()); //'value from options'
console.log(obj.getProperty2()); //false
//or in the constructor
obj2 = new MyClass(options);
console.log(obj2.getProperty1()); //'value from options'
console.log(obj2.getProperty2()); //false
Since version 1.1.0
we can use Configurable.withOptions to return a configured Configurable Trait.
This returns a trait that will look into the given options for the setter name given a key. If the key is not found, then it will default to set{Key} method.
Example:
The class will use addItems
instead of setItems since we want to add items to our items properties and will allow to pass an array or a single element.
MyClass.js
var cocktail = require('cocktail'),
Configurable = require('cocktail-trait-configurable');
cocktail.mix({
'@exports': module,
'@as' : 'class',
'@traits' : [
Configurable.withOptions({ items: 'addItems' })
],
'@properties': {
prop: 'default value'
},
constructor: function() {
this.items = [];
},
addItems: function (items) {
this.items.concat(items);
}
});
index.js
var MyClass = require('./MyClass'),
options, obj, obj2;
options = {
prop: 'value from options',
discarded: 'this should be discarded!',
items: [1,2,3,4]
});
obj = new MyClass();
//use the configure method publicly
obj.configure(options);
console.log(obj.getProp()); //'value from options'
console.log(obj.items); // [1,2,3,4]
The following methods will be publicly available on the host class:
configure(options)
: calls every available setter per each key defined in the the options object.
FAQs
Provides configurable method to set properties on host class by a given object
The npm package cocktail-trait-configurable receives a total of 0 weekly downloads. As such, cocktail-trait-configurable popularity was classified as not popular.
We found that cocktail-trait-configurable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.