0.0.3 • Published 11 years ago

relate v0.0.3

Weekly downloads
5
License
-
Repository
github
Last release
11 years ago

TOC

main

CoreObject

the constructor

creates an instance of CoreObject.

obj = new rel.CoreObject;
expect(obj).to.be.instanceof(rel.CoreObject);

can accept a map of initial key/values that get set on the instance.

var technology = 'JavaScript';
var domain = 'Everywhere';
obj = new rel.CoreObject({
    technology: technology,
    domain: domain
});

expected = [technology, domain];
actual = [obj.technology, obj.domain];

expect(actual).to.deep.equal(expected);

target/action (safe method invocation)

try

fails silently if the the target does not exist.

var target = undefined;
var actual = 'something';
actual = rel.try('singAndDance', target);
expect(actual).to.be.undefined;

invokes an action if an only if a method by that name exists on the target.

expected = ['didDoSomething'];
actual = [];

rel.try('doSomething', target, [actual]);

expect(actual).to.deep.equal(expected);

tryOnce

fails silently if the target does not exist.

var target = undefined;
var action = 'something';
action = rel.tryOnce('singAndDance', target);
expect(action).to.be.undefined;

only invokes action on a target if it has not been done before by tryOnce.

expected = ['didDoSomething'];
actual = [];

rel.tryOnce('doSomething', target, [actual]);
rel.tryOnce('doSomething', target, [actual]);

expect(actual).to.deep.equal(expected);

will invoke the same action on different targets.

expected = ['didDoSomething', 'didDoSomethingOnObj2'];
actual = [];

rel.tryOnce('doSomething', target, [actual]);
rel.tryOnce('doSomething', anotherTarget, [actual]);

expect(actual).to.deep.equal(expected);

forgetTryHistory

fails silently if the target does not exist.

var target = undefined;
var actual = 'something';
actual = rel.forgetTryHistory(target);
expect(actual).to.be.undefined;

fails silently if there is no "try history".

var target = {};
var actual = 'something';
actual = rel.forgetTryHistory(target);
expect(actual).to.be.undefined;

clears the invoke history on a target so that tryOnce can fire a previously fired action again.

var target = {
    doSomething: function(arr, index) {
        arr.push(['didDoSomething', index]);
    }
};

expected = [['didDoSomething', 0],
            ['didDoSomething', 2]];
actual = [];

rel.tryOnce('doSomething', target, [actual, 0]);
rel.tryOnce('doSomething', target, [actual, 1]);
rel.forgetTryHistory(target);
rel.tryOnce('doSomething', target, [actual, 2]);

expect(actual).to.deep.equal(expected);

derive

sets up a prototypal chain between parent and child.

rel.derive(Parent, Child);
expect(Child.prototype).to.be.an.instanceof(Parent);
expect(Child.prototype.constructor).to.equal(Child);

enables invocation of the constructor of parent.

var expected = { didInvokeParentConstructor: true,
                 didInvokeChildConstructor: true };
var actual;
var c;
var P = function() {
    this.didInvokeParentConstructor = true;
};

var C = function() {
    this._super.constructor();
    this.didInvokeChildConstructor = true;
};

rel.derive(P, C);

c = new C;
actual = { didInvokeParentConstructor: c.didInvokeParentConstructor,
           didInvokeChildConstructor: c.didInvokeChildConstructor };

expect(actual).to.deep.equal(expected);

adds methods to the child that can invoke methods on the parent (i.e. super functionality).

var child;
GrandParent.prototype.grow = function() { return 'grandParentDidGrow'; };

rel.derive(GrandParent, Parent, {
    grow: function() {
        return 'parentDidGrow ' + this._super.grow();
    }
});

rel.derive(Parent, Child, {
    grow: function() {
        return 'childDidGrow ' + this._super.grow();
    }
});

child = new Child;
expect(child.grow()).to.equal('childDidGrow parentDidGrow grandParentDidGrow');

adds properties to the child.

var child;
rel.derive(Parent, Child, {
    'invokePattern': 'async'
});

child = new Child;
expect(child.invokePattern).to.equal('async');