All files rule.js

100% Statements 6/6
100% Branches 0/0
100% Functions 2/2
100% Lines 6/6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32    1x                         2x 2x         1x 1x 1x                
import debug from 'debug'
 
var log = debug('facond.rule')
 
/**
 * Rules bind :js:class:`Action`
 */
class Rule {
  /**
   * Instanciate a Rule with a field name and a list of actions.
   *
   * @param {field} Field name as a string, with the form prefix.
   * @param {actions} List of actions for this rule.
   */
  constructor(field, actions) {
    this.field = field
    this.actions = actions
  }
 
  // Execute all actions of this rule.
  apply(form) {
    log('[Rule]', this, '.apply(', form, ')')
    for (var i=0; i<this.actions.length; i++) {
      this.actions[i].execute(form.field(this.field))
    }
  }
}
 
export {
  Rule,
}