Code coverage report for code/models/relationship.coffee

Statements: 100% (29 / 29)      Branches: 100% (0 / 0)      Functions: 100% (7 / 7)      Lines: 100% (23 / 23)      Ignored: none     

All files » code/models/ » relationship.coffee
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 471 1   1                 3 3 3     96 96 96 96 96 96     96 96     96     477 477     477 477   3 3 477     5    
math = require 'mathjs'  # For formula parsing...
tr   = require "../utils/translate"
 
module.exports = class Relationship
 
  @defaultText:       tr "~NODE-RELATION-EDIT.INCREASES"
  @defaultFormula:    "1 * in"
  @defaultGraphThumb: "TBD"
  @errValue:          -1
 
 
  @defaultErrHandler: (error,expr,vars)->
    log.error "Error in eval: #{Error}"
    log.error "Expression:    #{expr}"
    log.error "vars=#{vars}"
 
  constructor: (@opts={}) ->
    @text        = @opts.text       or Relationship.defaultText
    formula      = @opts.formula    or Relationship.defaultFormula
    @graphThumb  = @opts.graphThumb or Relationship.defaultGraphThumb
    @errHandler  = @opts.errHandler or Relationship.defaultErrHandler
    @hasError    = false
    @setFormula(formula)
 
  setFormula: (newf) ->
    @formula = newf
    @checkFormula()
 
  checkFormula: ->
    @evaluate(1, 1) #sets the @hasError flag if there is a problem
 
  evaluate: (inV,outV)->
    result = Relationship.errValue
    scope =
      in: inV
      out: outV
    try
      result = math.eval @formula, scope
    catch error
      @hasError = true
      @errHandler(error, @formula, inV, outV)
    result
 
  toExport: ->
    text        : @text
    formula     : @formula