pipeline.js

Context

function
Context()

The Context itself
not allowed to use as a function
@param {Object} config The object that is the source for the Context.

function Context(config) {
	var self = this;

	if (!(self instanceof Context)) {
		throw new Error('constructor is not a function');
	}
	self.overwrite(config);
}

overwrite

method
Context.prototype.overwrite()

Used to apply changes to context;

Context.prototype.overwrite = function(config) {
	var self = this;
	if (config) {
		var val;
		for (var prop in config) {
			val = config[prop];
			if (!reserved[prop]) {
				if (val !== undefined && val !== null)
					self[prop] = config[prop];
			}
		}
	}
};

getChilds

method
Context.prototype.getChilds() ->ArrayofContext

Returns list of child contexts.

Context.prototype.getChilds = function() {
	var self = this;
	if (!self.__children) {
		self.__children = [];
	}
	return self.__children;
};

getParent

method
Context.prototype.getParent() ->Context

Return parent Context

Context.prototype.getParent = function() {
	var self = this;
	return self.__parent;
};

hasChild

method
Context.prototype.hasChild() ->Boolean

checks wheater or not context has specific child context
it return true also if ctx is self;

Context.prototype.hasChild = function(ctx) {
	var self = this;
	if (ctx instanceof Context) {
		return ctx.__parent === self || self === ctx;
	}
};

ensure

method
Context.ensure()

Option name Type Description
ctx Object,Context

verified context

return

static function which ensures that the object is proper Type

Context.ensure = function(ctx) {
	if (!(ctx instanceof Context)) {
		return new Context(ctx);
	} else {
		return ctx;
	}
};

ensureIsChild

method
Context.prototype.ensureIsChild() ->Context

Option name Type Description
ctx Object,Context
return Context

Ensures that the context is the child of current context, and returns right context

Context.prototype.ensureIsChild = function(ctx) {
	var self = this;
	var lctx = Context.ensure(ctx);
	if (!self.hasChild(lctx)) {
		self.addChild(lctx);
	}
	return lctx;
};

addChild

method
Context.prototype.addChild()

Option name Type Description
ctx Context

new child context

Add child Context to current
!Note! All children contexts has parent list of error. This allow to be sure that any fork

Context.prototype.addChild = function(ctx) {
	var self = this;
	if (!self.hasChild(ctx)) {
		var child = Context.ensure(ctx);
		child.__parent = self;
		if (!self.__children) {
			self.__children = [];
		}
		self.__children.push(child);
	}
};

fork

method
Context.prototype.fork()

Option name Type Description
[config] Object,Context

new properties that must exists in new fork

Makes fork of current context and add it to current as a child context

Context.prototype.fork = function(config) {
	var self = this;
	var child = new Context(self);
	self.addChild(child);
	for (var p in config) {
		child[p] = config[p];
	}
	return child;
};

get

method
Context.prototype.get() ->Context

Option name Type Description
String at

path to context object that need to be a Context instance

return Context

Same but different as a fork. it make possible get piece of context as context;

Context.prototype.get = function(path) {
	var root = get(this, path);
	if (root instanceof Object) {
		var result = root;
		if (!(result instanceof Context)) {
			result = this.ensureIsChild(result);
			set(this, path, result);
		}
		return result;
	}
};

toObject

method
Context.prototype.toObject() ->Object

Option name Type Description
[clean] Boolean

true it need to clean object from referenced Types except Function and raw Object(js hash)

return Object

Convert context to raw Object;

Context.prototype.toObject = function(clean) {
	var self = this;
	var obj = {};
	for (var p in self) {
		if (!reserved[p]) {
			obj[p] = clone(self[p], clean);
		}
	}
	return obj;
};

toJSON

method
Context.prototype.toJSON() ->String

Conterts context to JSON

Context.prototype.toJSON = function() {
	var self = this;
	// always cleaning the object
	return JSON.stringify(self.toObject(true));
};