1.3.0 • Published 8 years ago

object-composer v1.3.0

Weekly downloads
1
License
ISC
Repository
-
Last release
8 years ago

ObjectComposer

Utility for managing micro service component dependencies. Runnable examples can be found in the examples directory.

##Add Constructor ObjectComposer aims to provide structure and utility in working with a microservice architecture. Service constructors and their dependencies are registered with the ObjectComposer instance. This data is cached and a hash returned for reference. Both the constructor's name or the returned hash can be used as identifiers for referencing the service.

Composer.add(function person(args, events) {

	var _name = 'joe'; //Define private variables.

	//Constructors must return an object definition.
	return {

		//Define getters and setters with their own logic to attach to object.
		get name() {
			return _name;
		},
		set name(value) {
			if (typeof value !== 'string') {
				_name = value;
			}
		}
	};
}, []);

Composer.add(function worker(args, events) {

	return {
		//Define methods to append to object.
		doSomething: function() {

			//Use inherited vars.
			console.log(this.name + ' is doing something.');
		}
	};
}, ['person']); //Define inheritance with array of type names.

##Remove Constructor To remove a constructor simple pass the relevant identifier to the remove method.

Composer.remove('worker');

##Creating Objects ObjectComposer provides three different methods for creating object instances: make, brew, and utility constructor. Both the 'make' and 'utility constructor' methods create instance of a registered type, while brew provides the ability to combine constructors in an ad hoc fashion. All three support the ability to pass an object to be used in initializing the returned object.

###The Base Object When ObjectComposer creates a registered object it first creates a based object to which it appends all the referenced functionality. This base object provides methods for accessing the pedigree of the returned object. The isA method facilities querying against the inheritance tree.

myObject.isA('product'); //Only supports string representation of constructor name

The inheritsFrom method provides an array of all the constructors utilized in creating the object.

###A Note About Inheritance When an object is created the referenced constructor is called and the inherited types are applied. This cycle is repeated down the inheritance hierarchy until all constructors have been applied. Each referenced constructor can only be called once in the composition of an object, with all subsequent calls being ignored.

###Make //Create a new copy of the object. Specify the type and pass object with values object should initialize with. let pizza = Composer.make('product', { name:'pizza' });

###Utility Constructor When an constructor is added a utility constructor method is appended to the ObjectComposer instance. This method can be use to create instances of the corresponding object.

let pizza = Composer.product({
	name:'pizza'
});
pizza.price = 5.50; //Set price.
pizza.purchased(); //Call methods.

###Brew When iterating on a problem it can be useful to informally mix services rather than rely on a rigid inheritance structure and object definition. The brew method affords this functionality. Simply provide the reference identifiers for the desired constructors and an object combining them will be created.

let pizza = Composer.brew(['product', 'shelved'], {
	name:'pizza',
	shelfId:1
});

##Events All objects created via ObjectComposer are wired for events. To emit an event in a constructor object simply use the events arg.

composer.add(function foo(args, events){
	return{
		call:function(){
		events.emit('called', {});
	}

},[]);

To subscribe to events on an object use the events property.

let myObj = compose.make('foo');
myObj.events.on('called', function(){
	console.log('I was called');
});

myObj.call(); //Logs 'I was called'

##To Do

  • provide built in serialization/deserializtion for created objects.
  • handle/prevent inheretence loops
  • optimization