Module: Cucumber::VariableExpression

Included in:
CiEnvironment
Defined in:
lib/cucumber/variable_expression.rb

Instance Method Summary collapse

Instance Method Details

#evaluate(expression, env) ⇒ Object



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
# File 'lib/cucumber/variable_expression.rb', line 5

def evaluate(expression, env)
  return nil if expression.nil?

  expression.gsub(/\${(.*?)(?:(?<!\\)\/(.*)\/(.*))?}/) do
    variable = Regexp.last_match(1)
    pattern = Regexp.last_match(2)
    replacement = Regexp.last_match(3)

    value = get_value(variable, env)
    raise "Undefined variable #{variable}" if value.nil?

    if pattern.nil?
      value
    else
      regexp = Regexp.new(pattern.gsub('\/', '/'))
      match = value.match(regexp)
      raise "No match for variable #{variable}" if match.nil?

      match[1..].each_with_index do |group, i|
        replacement = replacement.gsub("\\#{i + 1}", group)
      end
      replacement
    end
  end
rescue StandardError
  nil
end

#get_value(variable, env) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cucumber/variable_expression.rb', line 33

def get_value(variable, env)
  if variable.index('*')
    regexp = Regexp.new(variable.gsub('*', '.*'))
    # GoCD env var with dynamic "material" name
    # https://github.com/ashwanthkumar/gocd-build-github-pull-requests#github
    env.each do |name, value|
      return value if regexp.match?(name)
    end
  end
  env[variable]
end