Skip to content

Commit

Permalink
i believe this makes the default options clearer. also, cleaned up test
Browse files Browse the repository at this point in the history
harness modifications.
  • Loading branch information
carchrae committed Jun 26, 2013
1 parent 37d7c68 commit b16e1f0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
38 changes: 19 additions & 19 deletions lib/dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ function getGlobal(){

(function(dust) {

dust.optionsDefault = {
pathScope : "local" //sets the context path scope - other option is "global"
};

dust.setOptions = function(opts) {
dust.options = opts?opts:dust.optionsDefault;
if (dust.options.pathScope === "global") {
Context.prototype.getPath = getPathGlobal;
}
else if (dust.options.pathScope === "local") {
Context.prototype.getPath = getPathLocal;
}
};

dust.setOptions(dust.options);

dust.helpers = {};

dust.cache = {};
Expand Down Expand Up @@ -91,20 +107,6 @@ dust.isEmpty = function(value) {
return (!value);
};

dust.getOptions = function() {
return options;
}

dust.setOptions = function(opts) {
this.options = opts;
if (opts && opts.pathScope === "global") {
Context.prototype.getPath = getPathGlobal;
}
else if (opts && opts.pathScope === "local") {
Context.prototype.getPath = getPathLocal;
}
}

// apply the filter chain and return the output string
dust.filter = function(string, auto, filters) {
if (filters) {
Expand Down Expand Up @@ -171,10 +173,10 @@ Context.prototype.get = function(key) {

//no dot path resolution, function eval, or searching globals
function getPathLocal(cur, down) {
var ctx = this.stack, len = down.length;
var ctx = this.stack,
len = down.length;

if (cur && len === 0)
return ctx.head;
if (cur && len === 0) return ctx.head;
ctx = ctx.head;
var i = 0;
while (ctx && i < len) {
Expand Down Expand Up @@ -212,8 +214,6 @@ function getPathGlobal(cur, down) {
return ctx;
};

Context.prototype.getPath = getPathLocal;

Context.prototype.push = function(head, idx, len) {
return new Context(new Stack(head, this.stack, idx, len), this.global, this.blocks);
};
Expand Down
11 changes: 5 additions & 6 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
exports.coreSetup = function(suite, auto) {
auto.forEach(function(test) {
suite.test(test.name, function(){
var base = dust.makeBase({
glob: { globChild: "testGlobal"}
});
testRender(this, test.source, base.push(test.context), test.expected, test.options, test.error || {});

testRender(this, test.source, test.context, test.expected, test.options, test.base, test.error || {});
});
});

Expand Down Expand Up @@ -109,11 +105,14 @@ exports.coreSetup = function(suite, auto) {
});
}

function testRender(unit, source, context, expected, options, error) {
function testRender(unit, source, context, expected, options, baseContext, error) {
var name = unit.id;
try {
dust.loadSource(dust.compile(source, name));
dust.setOptions(options);
if (baseContext){
context = dust.makeBase(baseContext).push(context);
}
dust.render(name, context, function(err, output) {
unit.ifError(err);
unit.equals(output, expected);
Expand Down
2 changes: 2 additions & 0 deletions test/jasmine-test/spec/coreTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ var coreTests = [
name: "explicit context but gets value from global",
source: "{#data.A:B}Aname{name}{glob.globChild}{/data.A}",
options: {pathScope: "global"},
base: { glob: { globChild: "testGlobal"} },
context: { "data":{"A":{"name":"Al","list":[{"name": "Joe"},{"name": "Mary"}],"B":{"name":"Bob","Blist":["BB1","BB2"]}},C:{name:"cname"}} },
expected: "AnameAltestGlobal",
message: "should test access global despite explicit context"
Expand All @@ -761,6 +762,7 @@ var coreTests = [
name: "check nested ref in global works in global mode",
source: "{glob.globChild}",
options: {pathScope: "global"},
base: { glob: { globChild: "testGlobal"} },
context: { },
expected: "testGlobal",
message: "Should find glob.globChild which is in context.global"
Expand Down

0 comments on commit b16e1f0

Please sign in to comment.