Class: ParameterSubstitution::Expression

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression_list, context) ⇒ Expression

Returns a new instance of Expression.



9
10
11
12
# File 'lib/parameter_substitution/expression.rb', line 9

def initialize(expression_list, context)
  @expression_list = expression_list
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/parameter_substitution/expression.rb', line 7

def context
  @context
end

#expression_listObject (readonly)

Returns the value of attribute expression_list.



7
8
9
# File 'lib/parameter_substitution/expression.rb', line 7

def expression_list
  @expression_list
end

Instance Method Details

#evaluateObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/parameter_substitution/expression.rb', line 20

def evaluate
  if context.destination_encoding == :raw && @expression_list.size == 1
    # When using the destination encoding of raw, the output should preserve the type from the mapping if the substitution
    # is the full expression.  So the input of '<id>' with the mapping { 'id' => 1 } should return 1 and not "1".
    @expression_list.first.evaluate(:OutsideString, only_expression: true)
  else
    map_expressions_with_quote_tracking do |expression, inside_quotes|
      expression.evaluate(inside_quotes).to_s
    end.join("")
  end
end

#method_namesObject



45
46
47
48
49
# File 'lib/parameter_substitution/expression.rb', line 45

def method_names
  @expression_list.reduce([]) do |all_method_names, expression|
    all_method_names + methods_used_by_expression(expression)
  end
end

#methods_used_by_expression(expression) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/parameter_substitution/expression.rb', line 51

def methods_used_by_expression(expression)
  if (method_calls = expression.try(:method_calls))
    method_calls.reduce([]) do |all_method_call_names, method_call|
      all_method_call_names + [method_call.name.to_s] + method_call.arguments&.flat_map { |arg| arg.try(:method_names) }.compact # arg.try returns 'nil' when no methods are called; only method names needed
    end
  else
    []
  end
end

#parameter_and_method_warningsObject



36
37
38
39
# File 'lib/parameter_substitution/expression.rb', line 36

def parameter_and_method_warnings
  unknown_messages = unknown_parameter_messages + unknown_method_messages
  unknown_messages.empty? ? nil : unknown_messages.uniq
end

#substitution_parameter_namesObject



41
42
43
# File 'lib/parameter_substitution/expression.rb', line 41

def substitution_parameter_names
  @expression_list.map_compact(&:parameter_name)
end

#validateObject



14
15
16
17
18
# File 'lib/parameter_substitution/expression.rb', line 14

def validate
  validate_required_parameters
  validate_unknown_parameters
  validate_expressions
end

#warningsObject



32
33
34
# File 'lib/parameter_substitution/expression.rb', line 32

def warnings
  unknown_parameters_message
end