Socket
Socket
Sign inDemoInstall

cocktail-trait-configurable

Package Overview
Dependencies
1
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    cocktail-trait-configurable

Provides configurable method to set properties on host class by a given object


Version published
Weekly downloads
3
increased by50%
Maintainers
1
Install size
44.2 kB
Created
Weekly downloads
 

Readme

Source

cocktail-trait-configurable

A CocktailJS Trait Extension

Build Status NPM version

A trait to add configure(options) method that will call setters on each options key.

Install

npm install cocktail --save
npm install cocktail-trait-configurable --save

Trait requires (glue code)

None

Usage

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


Configuring the Configurable Trait

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]

API

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.
    • options: {Object} the object with values that are going to be applied.

Keywords

FAQs

Last updated on 25 May 2015

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc