Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ce query rc #185

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions lib/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var createRuleFromObject = (function () {
function parseRule(rule, conditions, identifiers, defined, name) {
if (rule.length) {
var r0 = rule[0];
if (r0 === "not" || r0 === "exists") {
if (r0 === "not" || r0 === "exists" || r0 === "collect" ) {
var temp = [];
rule.shift();
__resolveRule(rule, identifiers, temp, defined, name);
Expand All @@ -117,12 +117,19 @@ var createRuleFromObject = (function () {
}

return function (obj, defined, scope) {
var name = obj.name;
var name = obj.name,
options = obj.options || {};

options.scope = scope;

if (extd.isEmpty(obj)) {
throw new Error("Rule is empty");
}
var options = obj.options || {};
options.scope = scope;
if( obj.isQuery ) {
obj.options.arguments = obj.args;
options.scope = extd.merge(options.scope, defined || {});
return rules.createQuery(obj.name, obj.options, obj.constraints);
}
var constraints = obj.constraints || [], l = constraints.length;
if (!l) {
constraints = ["true"];
Expand All @@ -135,7 +142,10 @@ var createRuleFromObject = (function () {
forEach(constraints, function (rule) {
parseRule(rule, conditions, identifiers, defined, name);
});
return rules.createRule(name, options, conditions, parseAction(action, identifiers, defined, scope));
var ruleScope = merge(defined, scope);
var copyOptions = merge({}, options);
copyOptions.scope = ruleScope;
return rules.createRule(name, copyOptions, conditions, parseAction(action, identifiers, defined, scope));
};
})();

Expand All @@ -158,11 +168,13 @@ exports.compile = function (flowObj, options, cb, Container) {
throw new Error("Name must be present in JSON or options");
}
var flow = new Container(name);
var defined = merge({Array: Array, String: String, Number: Number, Boolean: Boolean, RegExp: RegExp, Date: Date, Object: Object}, options.define || {});
var defined = merge({Array: Array, String: String, Number: Number, Boolean: Boolean, RegExp: RegExp, Date: Date, Object: Object}, ( (options.define || options.defines || options.defined) || {}));
if (typeof Buffer !== "undefined") {
defined.Buffer = Buffer;
}
var scope = merge({console: console}, options.scope);
scope.queries = flow.__queries;
//
//add the anything added to the scope as a property
forEach(flowObj.scope, function (s) {
scope[s.name] = true;
Expand All @@ -187,7 +199,8 @@ exports.compile = function (flowObj, options, cb, Container) {
var rules = flowObj.rules;
if (rules.length) {
forEach(rules, function (rule) {
flow.__rules = flow.__rules.concat(createRuleFromObject(rule, defined, scope));
var instance = createRuleFromObject(rule, defined, scope);
flow.__addRule(instance);
});
}
if (cb) {
Expand Down
25 changes: 21 additions & 4 deletions lib/constraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ Constraint.extend({
}
}).as(exports, "HashConstraint");

Constraint.extend({
var FromConstraint = Constraint.extend({
instance: {
constructor: function (constraints, options) {
this.type = "from";
Expand All @@ -225,8 +225,8 @@ Constraint.extend({
return instanceOf(constraint, this._static) && this.get("alias") === constraint.get("alias") && deepEqual(this.constraints, constraint.constraints);
},

"assert": function (fact, fh) {
return this.constraints(fact, fh);
"assert": function (fact, fh, session) {
return this.constraints.call(session, fact, fh);
},

getters: {
Expand All @@ -236,7 +236,24 @@ Constraint.extend({
}

}
}).as(exports, "FromConstraint");
});
exports.FromConstraint = FromConstraint;

var QueryConstraint = FromConstraint.extend({
instance: {
constructor: function (constraints, options) {
this.type = "query";
this.constraints = constraintMatcher.getSourceMatcher(constraints, (options || {}), true);
extd.bindAll(this, ["assert"]);
},

"assert": function (fact, fh, session) {
return this.constraints.call(session, fact, fh);
}
}
});
exports.QueryConstraint = QueryConstraint;


Constraint.extend({
instance: {
Expand Down
Loading