pipeline.js

Wrap

function
Wrap()

Option name Type Description
config Object

configuration object

Wrap stage
configuration:

- prepare 
    used to prepera new context that fits wrapped stage 
- finalize
    used to write fill main context with result
function Wrap(config) {

	var self = this;

	if (!(self instanceof Wrap)) {
		throw new Error('constructor is not a function');
	}

	if (config && config.run instanceof Function) {
		config.stage = new Stage(config.run);
		delete config.run;
	}

	Stage.apply(self, arguments);

	if (config.stage instanceof Stage) {
		self.stage = config.stage;
	} else if (config.stage instanceof Function) {
		self.stage = new Stage(config.stage);
	} else {
		self.stage = new Empty();
	}

	if (config) {

		if (config.prepare instanceof Function) {
			self.prepare = config.prepare;
		}

		if (config.finalize instanceof Function) {
			self.finalize = config.finalize;
		}
	}
}

prepare

method
Wrap.prototype.prepare() ->Context

Option name Type Description
ctx Context
return Context

default prepare implementation

Wrap.prototype.prepare = function(ctx) {
	return ctx;
};

finalize

method
Wrap.prototype.finalize()

Option name Type Description
ctx Context
Context

default finalize implementation

Wrap.prototype.finalize = function(ctx, retCtx) {
	// by default the main context will be used to return;
	// so we do nothing here
};

reportName

method
Wrap.prototype.reportName()

override of reportName

Wrap.prototype.reportName = function() {
	return "Wrap:" + this.name;
};

compile

method
Wrap.prototype.compile()

override of compile

Wrap.prototype.compile = function() {
	var self = this;

	if (!self.name) {
		self.name = "success: " + self.stage.reportName() + " failure: " + self.overdue.reportName();
	}

	var run = function(ctx, done) {
		self.stage.execute(ctx, done);
	};

	self.run = run;
};

execute

method
Wrap.prototype.execute()

override of execute

Wrap.prototype.execute = function(_context, callback) {
	var self = this;

	_context = Context.ensure(_context);
	var context = self.prepare(_context);
	_context.ensureIsChild(context);

	if (!self.run) {
		self.compile();
	}

	var cb = function(err, context) {
		if (!err) {
			self.finalize(_context, context);
			callback(null, _context);
		} else {
			callback(err);
		}
	};

	Wrap.super_.prototype.execute.apply(self, [context, cb]);
};