Class: EquationEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/equation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default: {}, methods: {}) ⇒ EquationEngine

Returns a new instance of EquationEngine.



55
56
57
58
# File 'lib/equation.rb', line 55

def initialize(default: {}, methods: {})
  @parser = EquationParser.new
  @context = Context.new(default: default, methods: methods)
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



53
54
55
# File 'lib/equation.rb', line 53

def context
  @context
end

#parserObject

Returns the value of attribute parser.



53
54
55
# File 'lib/equation.rb', line 53

def parser
  @parser
end

Instance Method Details

#eval(rule:) ⇒ Object



71
72
73
# File 'lib/equation.rb', line 71

def eval(rule:)
  rule.value(ctx: @context)
end

#eval_with(rule:, values: {}) ⇒ Object



75
76
77
78
79
# File 'lib/equation.rb', line 75

def eval_with(rule:, values: {})
  rule.value(ctx: @context.tap { |x|
    x.transient_symbols = values
  })
end

#parse(rule:) ⇒ Object



60
61
62
63
64
65
# File 'lib/equation.rb', line 60

def parse(rule:)
  parsed_rule = @parser.parse(rule)
  raise "Parse Error: #{@parser.failure_reason}" unless parsed_rule

  parsed_rule
end

#parse_and_eval(rule:) ⇒ Object



67
68
69
# File 'lib/equation.rb', line 67

def parse_and_eval(rule:)
  parse(rule: rule).value(ctx: @context)
end