Invertable -> invertible. Other minor tidying.

This commit is contained in:
Michael Bayne
2014-03-11 07:43:44 -07:00
parent 2251ac1ed2
commit 59e06b24c9
3 changed files with 25 additions and 31 deletions
+4 -5
View File
@@ -428,12 +428,12 @@ inverse section rather than having the section omitted altogether. This allows
for proper conditional substitution when statically translating templates into for proper conditional substitution when statically translating templates into
other languages or contexts: other languages or contexts:
String template = "{{#condition}}result if true{{/condition}}\n{{^condition}}result if false{{/condition}}"; String template = "{{#condition}}result if true{{/condition}}\n" +
"{{^condition}}result if false{{/condition}}";
Mustache.compiler().compile(template).execute(new Object() { Mustache.compiler().compile(template).execute(new Object() {
Mustache.InvertableLambda condition = new Mustache.InvertableLambda() { Mustache.InvertableLambda condition = new Mustache.InvertableLambda() {
@Override @Override
public void execute(Template.Fragment frag, Writer out) public void execute(Template.Fragment frag, Writer out) throws IOException {
throws IOException {
// this method is executed when the lambda is referenced in a normal section // this method is executed when the lambda is referenced in a normal section
out.write("if (condition) {console.log(\""); out.write("if (condition) {console.log(\"");
out.write(toJavaScriptLiteral(frag.execute())); out.write(toJavaScriptLiteral(frag.execute()));
@@ -441,8 +441,7 @@ other languages or contexts:
} }
@Override @Override
public void executeInverse(Template.Fragment frag, Writer out) public void executeInverse(Template.Fragment frag, Writer out) throws IOException {
throws IOException {
// this method is executed when the lambda is referenced in an inverse section // this method is executed when the lambda is referenced in an inverse section
out.write("if (!condition) {console.log(\""); out.write("if (!condition) {console.log(\"");
out.write(toJavaScriptLiteral(frag.execute())); out.write(toJavaScriptLiteral(frag.execute()));
@@ -224,7 +224,8 @@ public class Mustache
void execute (Template.Fragment frag, Writer out) throws IOException; void execute (Template.Fragment frag, Writer out) throws IOException;
} }
public interface InvertableLambda extends Lambda /** Handles lambdas that are also invoked for inverse sections.. */
public interface InvertibleLambda extends Lambda
{ {
/** Executes this lambda on the supplied template fragment, when the lambda is used in an /** Executes this lambda on the supplied template fragment, when the lambda is used in an
* inverse section. The lambda should write its results to {@code out}. * inverse section. The lambda should write its results to {@code out}.
@@ -784,9 +785,9 @@ public class Mustache
if (!(Boolean)value) { if (!(Boolean)value) {
executeSegs(tmpl, ctx, out); executeSegs(tmpl, ctx, out);
} }
} else if (value instanceof InvertableLambda) { } else if (value instanceof InvertibleLambda) {
try { try {
((InvertableLambda)value).executeInverse(tmpl.createFragment(_segs, ctx), out); ((InvertibleLambda)value).executeInverse(tmpl.createFragment(_segs, ctx), out);
} catch (IOException ioe) { } catch (IOException ioe) {
throw new MustacheException(ioe); throw new MustacheException(ioe);
} }
@@ -629,30 +629,24 @@ public class MustacheTest
})); }));
} }
@Test public void testInvertableLambda () { @Test public void testInvertibleLambda () {
test("positive = positive, negative = negative, simple lambdas do still work", test("positive = positive, negative = negative, simple lambdas do still work",
"{{#invertable}}positive{{/invertable}}, {{^invertable}}negative{{/invertable}}, simple lambdas do {{^simple}}NOT {{/simple}}still work", "{{#invertible}}positive{{/invertible}}, {{^invertible}}negative{{/invertible}}, " +
context("invertable", new Mustache.InvertableLambda() { "simple lambdas do {{^simple}}NOT {{/simple}}still work",
@Override context("invertible", new Mustache.InvertibleLambda() {
public void execute(Template.Fragment frag, Writer out) public void execute (Template.Fragment frag, Writer out) throws IOException {
throws IOException { out.write("positive = ");
out.write("positive = "); frag.execute(out);
frag.execute(out); }
} public void executeInverse (Template.Fragment frag, Writer out) throws IOException {
out.write("negative = ");
@Override frag.execute(out);
public void executeInverse(Template.Fragment frag, Writer out) }
throws IOException { }, "simple", new Mustache.Lambda() {
out.write("negative = "); public void execute (Template.Fragment frag, Writer out) throws IOException {
frag.execute(out); frag.execute(out);
} }
}, "simple", new Mustache.Lambda() { }));
@Override
public void execute(Template.Fragment frag, Writer out)
throws IOException {
frag.execute(out);
}
}));
} }
@Test public void testNonStandardDefaultDelims () { @Test public void testNonStandardDefaultDelims () {