Class: Gitlab::Ci::Pipeline::Expression::Lexeme::Matches

Inherits:
LogicalOperator show all
Defined in:
lib/gitlab/ci/pipeline/expression/lexeme/matches.rb

Constant Summary collapse

PATTERN =
/=~/

Constants inherited from Operator

Operator::OperatorError

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from LogicalOperator

#initialize, #inspect, type

Methods inherited from Operator

type

Methods inherited from Base

consume?, #name, pattern, scan

Constructor Details

This class inherits a constructor from Gitlab::Ci::Pipeline::Expression::Lexeme::LogicalOperator

Class Method Details

.build(_value, behind, ahead) ⇒ Object



24
25
26
# File 'lib/gitlab/ci/pipeline/expression/lexeme/matches.rb', line 24

def self.build(_value, behind, ahead)
  new(behind, ahead)
end

.precedenceObject



28
29
30
# File 'lib/gitlab/ci/pipeline/expression/lexeme/matches.rb', line 28

def self.precedence
  10 # See: https://ruby-doc.org/core-2.5.0/doc/syntax/precedence_rdoc.html
end

Instance Method Details

#evaluate(variables = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gitlab/ci/pipeline/expression/lexeme/matches.rb', line 11

def evaluate(variables = {})
  text = @left.evaluate(variables)
  regexp = @right.evaluate(variables)

  return false unless regexp

  # All variables are evaluated as strings, even if they are regexp strings.
  # So, we need to convert them to regexp objects.
  regexp = Lexeme::Pattern.build_and_evaluate(regexp, variables)

  regexp.scan(text.to_s).present?
end