Skip to content

Commit 029bae1

Browse files
Matt CopenhaverMatt Copenhaver
authored andcommitted
Adds name to Rule and RuleResult
1 parent 2d6e834 commit 029bae1

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

src/rule-result.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import deepClone from 'clone'
44

55
export default class RuleResult {
6-
constructor (conditions, event, priority) {
6+
constructor (conditions, event, priority, name) {
77
this.conditions = deepClone(conditions)
88
this.event = deepClone(event)
99
this.priority = deepClone(priority)
10+
this.name = deepClone(name)
1011
this.result = null
1112
}
1213

@@ -19,6 +20,7 @@ export default class RuleResult {
1920
conditions: this.conditions.toJSON(false),
2021
event: this.event,
2122
priority: this.priority,
23+
name: this.name,
2224
result: this.result
2325
}
2426
if (stringify) {

src/rule.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Rule extends EventEmitter {
1414
* @param {string} options.event.type - name of event to emit
1515
* @param {string} options.event.params - parameters to pass to the event listener
1616
* @param {Object} options.conditions - conditions to evaluate when processing this rule
17+
* @param {string} options.name - identifier for a particular rule, particularly valuable in RuleResult output
1718
* @return {Rule} instance
1819
*/
1920
constructor (options) {
@@ -30,6 +31,9 @@ class Rule extends EventEmitter {
3031
if (options && options.onFailure) {
3132
this.on('failure', options.onFailure)
3233
}
34+
if (options && options.name) {
35+
this.setName(options.name)
36+
}
3337

3438
let priority = (options && options.priority) || 1
3539
this.setPriority(priority)
@@ -49,6 +53,18 @@ class Rule extends EventEmitter {
4953
return this
5054
}
5155

56+
/**
57+
* Sets the name of the rule
58+
* @param {string} name - only non-empty strings are allowed
59+
*/
60+
setName (name) {
61+
if (!name || typeof name !== 'string') {
62+
throw new Error('Rule "name" must be either undefined or a non-empty string')
63+
}
64+
this.name = name
65+
return this
66+
}
67+
5268
/**
5369
* Sets the conditions to run when evaluating the rule.
5470
* @param {object} conditions - conditions, root element must be a boolean operator
@@ -91,7 +107,8 @@ class Rule extends EventEmitter {
91107
let props = {
92108
conditions: this.conditions.toJSON(false),
93109
priority: this.priority,
94-
event: this.event
110+
event: this.event,
111+
name: this.name
95112
}
96113
if (stringify) {
97114
return JSON.stringify(props)
@@ -131,7 +148,7 @@ class Rule extends EventEmitter {
131148
* @return {Promise(RuleResult)} rule evaluation result
132149
*/
133150
evaluate (almanac) {
134-
let ruleResult = new RuleResult(this.conditions, this.event, this.priority)
151+
let ruleResult = new RuleResult(this.conditions, this.event, this.priority, this.name)
135152

136153
/**
137154
* Evaluates the rule conditions

test/rule.test.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Rule', () => {
1212
})
1313

1414
describe('constructor()', () => {
15-
it('can be initialized with priority, conditions, and event', () => {
15+
it('can be initialized with priority, conditions, event, and name', () => {
1616
let condition = {
1717
all: [ Object.assign({}, conditionBase) ]
1818
}
@@ -23,12 +23,14 @@ describe('Rule', () => {
2323
conditions: condition,
2424
event: {
2525
type: 'awesome'
26-
}
26+
},
27+
name: 'testName'
2728
}
2829
let rule = new Rule(opts)
2930
expect(rule.priority).to.eql(opts.priority)
3031
expect(rule.conditions).to.eql(opts.conditions)
3132
expect(rule.event).to.eql(opts.event)
33+
expect(rule.name).to.eql(opts.name)
3234
})
3335

3436
it('it can be initialized with a json string', () => {
@@ -42,13 +44,15 @@ describe('Rule', () => {
4244
conditions: condition,
4345
event: {
4446
type: 'awesome'
45-
}
47+
},
48+
name: 'testName'
4649
}
4750
let json = JSON.stringify(opts)
4851
let rule = new Rule(json)
4952
expect(rule.priority).to.eql(opts.priority)
5053
expect(rule.conditions).to.eql(opts.conditions)
5154
expect(rule.event).to.eql(opts.event)
55+
expect(rule.name).to.eql(opts.name)
5256
})
5357
})
5458

@@ -115,6 +119,25 @@ describe('Rule', () => {
115119
})
116120
})
117121

122+
describe('setName', () => {
123+
it('defaults to undefined', () => {
124+
expect(rule.name).to.equal(undefined)
125+
})
126+
127+
it('allows the name to be set', () => {
128+
rule.setName('Test Name')
129+
expect(rule.name).to.equal('Test Name')
130+
})
131+
132+
it('errors if name is an empty string', () => {
133+
expect(rule.setName.bind(null, '')).to.throw(/Rule "name" must be either undefined or a non-empty string/)
134+
})
135+
136+
it('errors if name is not a string', () => {
137+
expect(rule.setName.bind(null, 3)).to.throw(/Rule "name" must be either undefined or a non-empty string/)
138+
})
139+
})
140+
118141
describe('priotizeConditions()', () => {
119142
let conditions = [{
120143
fact: 'age',
@@ -185,30 +208,34 @@ describe('Rule', () => {
185208
path: '.id'
186209
}]
187210
}
211+
let name = 'testName'
188212
let rule
189213
beforeEach(() => {
190214
rule = new Rule()
191215
rule.setConditions(conditions)
192216
rule.setPriority(priority)
193217
rule.setEvent(event)
218+
rule.setName(name)
194219
})
195220

196221
it('serializes itself', () => {
197222
let json = rule.toJSON(false)
198-
expect(Object.keys(json).length).to.equal(3)
223+
expect(Object.keys(json).length).to.equal(4)
199224
expect(json.conditions).to.eql(conditions)
200225
expect(json.priority).to.eql(priority)
201226
expect(json.event).to.eql(event)
227+
expect(json.name).to.eql(name)
202228
})
203229

204230
it('serializes itself as json', () => {
205231
let jsonString = rule.toJSON()
206232
expect(jsonString).to.be.a('string')
207233
let json = JSON.parse(jsonString)
208-
expect(Object.keys(json).length).to.equal(3)
234+
expect(Object.keys(json).length).to.equal(4)
209235
expect(json.conditions).to.eql(conditions)
210236
expect(json.priority).to.eql(priority)
211237
expect(json.event).to.eql(event)
238+
expect(json.name).to.eql(name)
212239
})
213240

214241
it('rehydrates itself using a JSON string', () => {
@@ -218,6 +245,7 @@ describe('Rule', () => {
218245
expect(hydratedRule.conditions).to.eql(rule.conditions)
219246
expect(hydratedRule.priority).to.eql(rule.priority)
220247
expect(hydratedRule.event).to.eql(rule.event)
248+
expect(hydratedRule.name).to.eql(rule.name)
221249
})
222250

223251
it('rehydrates itself using an object from JSON.parse()', () => {
@@ -228,6 +256,8 @@ describe('Rule', () => {
228256
expect(hydratedRule.conditions).to.eql(rule.conditions)
229257
expect(hydratedRule.priority).to.eql(rule.priority)
230258
expect(hydratedRule.event).to.eql(rule.event)
259+
expect(hydratedRule.name).to.eql(rule.name)
231260
})
232261
})
233262
})
263+

0 commit comments

Comments
 (0)