Skip to content

Commit

Permalink
Use meld() instead of meld.add() in docs and tests. See #18
Browse files Browse the repository at this point in the history
  • Loading branch information
briancavalier committed Apr 8, 2013
1 parent e40b94f commit 95eb7fe
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 36 deletions.
20 changes: 11 additions & 9 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* [meld.afterReturning](#meldafterreturning)
* [meld.afterThrowing](#meldafterthrowing)
* [meld.after](#meldafter)
1. [Adding Multiple Advices](#adding-multiple-advices)
* [meld.add](#meldadd)
1. [Adding Aspects](#adding-aspects)
* [meld](#meld)
1. [Removing Method Advice](#removing-method-advice)
1. [Accessing the Joinpoint](#accessing-the-joinpoint)
* [meld.joinpoint](#meldjoinpoint)
Expand Down Expand Up @@ -175,13 +175,15 @@ Returns a new function or constructor that calls `afterFunction` after the origi

In the specific case of a constructor, the newly constructed instance acts as the return value, and will be the argument provided to the `afterFunction` when the constructor returns successfully.

# Adding Multiple Advices
# Adding Aspects

Meld.js allows you to add any number of advices to a method, function, or constructor. In addition to adding them individually, as [shown here](#advising-methods), using the individual advice methods (e.g. meld.before, meld.after, etc.), you can also add several advices at once using `meld.add()`.
Meld.js allows you to add any number of advices to a method, function, or constructor. In addition to adding them individually, as [shown here](#advising-methods), using the individual advice methods (e.g. meld.before, meld.after, etc.), you can also add several advices at once using `meld()`.

For example, the [bundled aspects](aspects.md) are implemented this way, and can be added using `meld.add()`.
For example, the [bundled aspects](aspects.md) are implemented this way, and can be added using `meld()`.

## meld.add
## meld

**DEPRECATED ALIAS:** meld.add()

```js
// Supply any or all of the advice types at once
Expand All @@ -197,15 +199,15 @@ var advices = {
}
}

var remover = meld.add(object, match, advices);
var remover = meld(object, match, advices);
```

Adds multiple advices to each matched method.

```js
var advisedFunction = meld.add(functionToAdvise, advices);
var advisedFunction = meld(functionToAdvise, advices);

var AdvisedConstructor = meld.add(ConstructorToAdvise, advices);
var AdvisedConstructor = meld(ConstructorToAdvise, advices);
```

Adds multiple advices to the supplied function or constructor.
Expand Down
20 changes: 10 additions & 10 deletions docs/aspects.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# Aspects

Besides adding individual advices, such as before, afterReturning, etc., meld supports adding aspects, which are essentially one or more pieces of advice that work together to implement some functionality. Aspects can be added using [meld.add](api.md#adding-multiple-advices)
Besides adding individual advices, such as before, afterReturning, etc., meld supports adding aspects, which are essentially one or more pieces of advice that work together to implement some functionality. Aspects can be added using [meld](api.md#adding-aspects)

Meld comes with several aspects, in the `aspect` dir, that you can use, and that serve as examples for implementing your own.

Expand All @@ -16,9 +16,9 @@ Meld comes with several aspects, in the `aspect` dir, that you can use, and that
```js
var trace = require('meld/aspect/trace');

var traced = meld.add(object, pointcut, trace());
var traced = meld(object, pointcut, trace());
// or
var traced = meld.add(func, trace());
var traced = meld(func, trace());
```

Creates an aspect that traces method and function calls, and to report when they are called, their parameters, and whether each returns successfully or throws an exception, with the associated return value or throw exception. By default, the trace aspect uses a builtin reporter that simply logs information using `console.log`.
Expand Down Expand Up @@ -52,17 +52,17 @@ var myReporter = {
}
}

var traced = meld.add(object, pointcut, trace(myReporter));
var traced = meld(object, pointcut, trace(myReporter));
```

# aspect/memoize

```js
var memoize = require('meld/aspect/memoize');

var memoized = meld.add(object, pointcut, memoize());
var memoized = meld(object, pointcut, memoize());
// or
var memoized = meld.add(func, memoize());
var memoized = meld(func, memoize());
```

Creates an aspect that [memoizes](http://en.wikipedia.org/wiki/Memoization) a method or function. The first call to any memoized method or function with a specific set of params will always execute the original method/function. The result will be stored in a table for fast lookup the next time the method/function is invoked *with the same params*. Thus, subsequent calls to the memoized method/function with previously used params will always return a value from the map.
Expand All @@ -78,17 +78,17 @@ function myKeyGenerator(paramsArray) {
return key;
}

var memoized = meld.add(object, pointcut, memoize(myKeyGenerator));
var memoized = meld(object, pointcut, memoize(myKeyGenerator));
```

# aspect/cache

```js
var cache = require('meld/aspect/cache');

var cached = meld.add(object, pointcut, cache(storage));
var cached = meld(object, pointcut, cache(storage));
// or
var cached = meld.add(func, cache(storage));
var cached = meld(func, cache(storage));
```

Creates an aspect that can help in performing more sophisticated caching than the [memoize aspect](#aspectmemoize). You must supply a cache storage object that implements the following API:
Expand Down Expand Up @@ -121,5 +121,5 @@ function myKeyGenerator(paramsArray) {
return key;
}

var cached = meld.add(object, pointcut, cache(storage, myKeyGenerator));
var cached = meld(object, pointcut, cache(storage, myKeyGenerator));
```
8 changes: 4 additions & 4 deletions test/aspect/cache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ buster.testCase('aspect/cache', {
set: noop
};

meld.add(advised, 'method', createCache(cache));
meld(advised, 'method', createCache(cache));

advised.method(param);

Expand All @@ -42,7 +42,7 @@ buster.testCase('aspect/cache', {
get: this.stub().returns(sentinel)
};

meld.add(advised, 'method', createCache(cache));
meld(advised, 'method', createCache(cache));

assert.same(advised.method(param), sentinel);

Expand All @@ -60,7 +60,7 @@ buster.testCase('aspect/cache', {
set: this.spy()
};

meld.add(advised, 'method', createCache(cache));
meld(advised, 'method', createCache(cache));

advised.method(param);

Expand All @@ -80,7 +80,7 @@ buster.testCase('aspect/cache', {
get: this.stub()
};

meld.add(advised, 'method', createCache(cache, stubKeyGenerator));
meld(advised, 'method', createCache(cache, stubKeyGenerator));

advised.method(param, 1);

Expand Down
8 changes: 4 additions & 4 deletions test/aspect/memoize-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
spy = this.spy();
advised = { method: spy };

meld.add(advised, 'method', createMemoizer());
meld(advised, 'method', createMemoizer());

advised.method(param);

Expand All @@ -29,7 +29,7 @@
spy = this.spy();
advised = { method: spy };

meld.add(advised, 'method', createMemoizer());
meld(advised, 'method', createMemoizer());

advised.method(param);
advised.method(param);
Expand All @@ -44,7 +44,7 @@
spy = this.spy();
advised = { method: spy };

meld.add(advised, 'method', createMemoizer());
meld(advised, 'method', createMemoizer());

advised.method(param, 1);
advised.method(param, 2);
Expand All @@ -58,7 +58,7 @@
stubKeyGenerator = this.stub().returns('the key');
advised = { method: function(/*x, y*/) {} };

meld.add(advised, 'method', createMemoizer(stubKeyGenerator));
meld(advised, 'method', createMemoizer(stubKeyGenerator));

advised.method(param, 1);

Expand Down
6 changes: 3 additions & 3 deletions test/aspect/trace-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ buster.testCase('aspect/trace', {
advised = { method: spy };
reporter = { onCall: this.spy() };

meld.add(advised, 'method', createTracer(reporter));
meld(advised, 'method', createTracer(reporter));

advised.method(sentinel);

Expand All @@ -33,7 +33,7 @@ buster.testCase('aspect/trace', {
};
reporter = { onReturn: this.spy() };

meld.add(advised, 'method', createTracer(reporter));
meld(advised, 'method', createTracer(reporter));

advised.method();

Expand All @@ -51,7 +51,7 @@ buster.testCase('aspect/trace', {
};
reporter = { onThrow: this.spy() };

meld.add(advised, 'method', createTracer(reporter));
meld(advised, 'method', createTracer(reporter));

assert.exception(function() {
advised.method();
Expand Down
2 changes: 1 addition & 1 deletion test/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ buster.testCase('functions', {
spyAround = this.spy();
spyAfter = this.spy();

advised = meld.add(f, {
advised = meld(f, {
before: spyBefore,
around: function(call) {
var ret = call.proceed();
Expand Down
8 changes: 4 additions & 4 deletions test/pointcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ buster.testCase('pointcuts', {

before = this.spy();

meld.add(target, 'method1', {
meld(target, 'method1', {
before: before
});

Expand All @@ -43,7 +43,7 @@ buster.testCase('pointcuts', {

before = this.spy();

meld.add(target, ['method1', 'method3'], {
meld(target, ['method1', 'method3'], {
before: before
});

Expand Down Expand Up @@ -73,7 +73,7 @@ buster.testCase('pointcuts', {

before = this.spy();

meld.add(target, /method[13]/, {
meld(target, /method[13]/, {
before: before
});

Expand Down Expand Up @@ -103,7 +103,7 @@ buster.testCase('pointcuts', {

before = this.spy();

meld.add(target,
meld(target,
function() {
return ['method1', 'method3'];
},
Expand Down
2 changes: 1 addition & 1 deletion test/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ buster.testCase('remove', {

fixture = new Fixture();
fixture.method = this.spy();
ref = meld.add(fixture, 'method', aspect);
ref = meld(fixture, 'method', aspect);

fixture.method();

Expand Down

0 comments on commit 95eb7fe

Please sign in to comment.