Class: ActsAsCalculatorEditor::ValidateExpression

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

Overview

What a live expression field can be told about a formula before it is published, with no evaluation and nothing persisted (docs/core-gem-contract.md §4.1).

BuildCalculator.() hands back a Dentaku calculator with the gem's function registry already installed, so #ast catches unbalanced parentheses, malformed operators and undefined function names — including whatever the host registered — while #identifiers lists every variable the expression could reach, syntactically, without evaluating a single branch.

What this deliberately does not claim: whether the host's model actually responds to an attribute or method variable. That is only knowable when a real record is in hand, and saying otherwise would promise a guarantee the check does not make.

This belongs upstream next to EvaluateExpression; it lives here because the core gem has no ValidateExpression Decree yet.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression:, variables: [], scope: nil, owner: nil) ⇒ ValidateExpression

Returns a new instance of ValidateExpression.



24
25
26
27
28
29
# File 'lib/acts_as_calculator_editor/validate_expression.rb', line 24

def initialize(expression:, variables: [], scope: nil, owner: nil)
  @expression = expression.to_s
  @variables = Array(variables).map { |variable| ActsAsCalculator::CastVariableAttributes.(variable) }
  @scope = (scope.presence || ActsAsCalculator::DEFAULT_SCOPE).to_s
  @owner = owner
end

Class Method Details

.callObject



20
21
22
# File 'lib/acts_as_calculator_editor/validate_expression.rb', line 20

def self.call(...)
  new(...).call
end

Instance Method Details

#callObject



31
32
33
34
35
36
# File 'lib/acts_as_calculator_editor/validate_expression.rb', line 31

def call
  return blank if expression.strip.empty?
  return unparsable if syntax_error

  { valid: problems.empty?, errors: problems, warnings:, identifiers: }
end