Permalink
Browse files

Replace MethodChain._() with MethodChain.__() so as not to conflict w…

…ith Underscore.
  • Loading branch information...
1 parent efd5b25 commit f0539f312f09be91bee9f5e75cf4fdc0acc80fd8 @jcoglan committed Jan 21, 2014
Showing with 19 additions and 19 deletions.
  1. +8 −8 site/src/pages/methodchain.haml
  2. +2 −2 source/method_chain.js
  3. +9 −9 test/specs/method_chain_spec.js
@@ -55,26 +55,26 @@
There are a few reserved methods on @MethodChain@ objects that you cannot use
for chaining:
- * @_()@ - one underscore - more on this in a second
+ * @__()@ - two underscores - more on this in a second
* @____()@ - four underscores, used for storing method calls
* @__exec__()@ - executes the stored chain against its argument
* @toFunction()@ - returns a function that executes the chain when called
- h3. Changing scope using @_()@
+ h3. Changing scope using @__()@
- All chains have a method called @_()@ (that's one underscore) that you can use
- to change the scope of the chain. By default, each method in the chain is
- called on the return value of the previous method, but @_()@ lets you insert
+ All chains have a method called @__()@ (that's two underscores) that you can
+ use to change the scope of the chain. By default, each method in the chain is
+ called on the return value of the previous method, but @__()@ lets you insert
objects into the chain so that subsequent methods get called on said object.
- @_()@ also lets you insert anonymous functions into the chain. Within each
+ @__()@ also lets you insert anonymous functions into the chain. Within each
function, @this@ refers to the return value of the previous method. Putting
all this together, you could do:
<pre>var chain = new MethodChain();
- chain._(document).getElementsByTagName('p')
- ._(function() { console.log(this) });</pre>
+ chain.__(document).getElementsByTagName('p')
+ .__(function() { console.log(this) });</pre>
When we execute @chain@, the net effect is the same as:
View
@@ -48,7 +48,7 @@ MethodChain.toString = function() {
};
MethodChain.prototype = {
- _: function() {
+ __: function() {
var base = arguments[0],
args, i, n;
@@ -121,7 +121,7 @@ JS.Kernel.include({
return chain;
},
- _: function() {
+ __: function() {
var base = arguments[0],
args = [],
i, n;
@@ -88,16 +88,16 @@ JS.ENV.MethodChainSpec = JS.Test.describe(MethodChain, function() { with(this) {
}})
}})
- describe("#_", function() { with(this) {
+ describe("#__", function() { with(this) {
describe("in MethodChain", function() { with(this) {
describe("with an object", function() { with(this) {
it("changes the base object for the rest of the chain", function() { with(this) {
- chain._(["another", "list"]).sort()
+ chain.__(["another", "list"]).sort()
assertEqual( ["another", "list"], chain.__exec__() )
}})
it("does not stop previous calls going to the old base object", function() { with(this) {
- chain.call()._(["another", "list"]).sort()
+ chain.call().__(["another", "list"]).sort()
assert( !list.called )
chain.__exec__()
assert( list.called )
@@ -106,17 +106,17 @@ JS.ENV.MethodChainSpec = JS.Test.describe(MethodChain, function() { with(this) {
describe("with a function", function() { with(this) {
it("adds a function to the chain", function() { with(this) {
- chain._(function() { return this.slice(1) })
+ chain.__(function() { return this.slice(1) })
assertEqual( ["bar"], chain.__exec__() )
}})
it("adds a function with arguments to the chain", function() { with(this) {
- chain._(function(method, arg) { return this[method](arg) }, "slice", 1)
+ chain.__(function(method, arg) { return this[method](arg) }, "slice", 1)
assertEqual( ["bar"], chain.__exec__() )
}})
it("uses the return value as the new base object", function() { with(this) {
- chain._(function() { return "word" }).toUpperCase()
+ chain.__(function() { return "word" }).toUpperCase()
assertEqual( "WORD", chain.__exec__() )
}})
}})
@@ -128,15 +128,15 @@ JS.ENV.MethodChainSpec = JS.Test.describe(MethodChain, function() { with(this) {
}})
it("returns the object given to it", function() { with(this) {
- assertSame( list, widget._(list) )
+ assertSame( list, widget.__(list) )
}})
it("applies a function to the receiver", function() { with(this) {
- assertEqual( "Nick", widget._(function() { return this.name }) )
+ assertEqual( "Nick", widget.__(function() { return this.name }) )
}})
it("applies a function with arguments to the receiver", function() { with(this) {
- assertEqual( "Nick and David", widget._(function(name) { return this.name + " and " + name }, "David") )
+ assertEqual( "Nick and David", widget.__(function(name) { return this.name + " and " + name }, "David") )
}})
}})
}})

0 comments on commit f0539f3

Please sign in to comment.