Class: Gitlab::Ci::Pipeline::Expression::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/ci/pipeline/expression/statement.rb

Constant Summary collapse

StatementError =
Class.new(Expression::ExpressionError)

Instance Method Summary collapse

Constructor Details

#initialize(statement, variables = {}) ⇒ Statement

Returns a new instance of Statement.


10
11
12
13
# File 'lib/gitlab/ci/pipeline/expression/statement.rb', line 10

def initialize(statement, variables = {})
  @lexer = Expression::Lexer.new(statement)
  @variables = variables.with_indifferent_access
end

Instance Method Details

#evaluateObject


21
22
23
# File 'lib/gitlab/ci/pipeline/expression/statement.rb', line 21

def evaluate
  parse_tree.evaluate(@variables.to_h)
end

#parse_treeObject

Raises:


15
16
17
18
19
# File 'lib/gitlab/ci/pipeline/expression/statement.rb', line 15

def parse_tree
  raise StatementError if @lexer.lexemes.empty?

  Expression::Parser.new(@lexer.tokens).tree
end

#truthful?Boolean

Returns:

  • (Boolean)

25
26
27
28
29
# File 'lib/gitlab/ci/pipeline/expression/statement.rb', line 25

def truthful?
  evaluate.present?
rescue Expression::ExpressionError
  false
end

#valid?Boolean

Returns:

  • (Boolean)

31
32
33
34
35
36
# File 'lib/gitlab/ci/pipeline/expression/statement.rb', line 31

def valid?
  evaluate
  parse_tree.is_a?(Lexeme::Base)
rescue Expression::ExpressionError
  false
end