Class: ParameterSubstitution::SubstitutionExpression

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameter_name, method_calls, context) ⇒ SubstitutionExpression

Returns a new instance of SubstitutionExpression.



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

def initialize(parameter_name, method_calls, context)
  @context = context
  @parameter_name, @method_calls = fixup_name_and_method_calls(parameter_name, method_calls)
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#method_callsObject

Returns the value of attribute method_calls.



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

def method_calls
  @method_calls
end

#parameter_nameObject

Returns the value of attribute parameter_name.



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

def parameter_name
  @parameter_name
end

Instance Method Details

#ends_inside_quotes(started_inside_quotes:) ⇒ Object



46
47
48
# File 'lib/parameter_substitution/substitution_expression.rb', line 46

def ends_inside_quotes(started_inside_quotes:)
  started_inside_quotes
end

#evaluate(inside_quotes, only_expression: false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/parameter_substitution/substitution_expression.rb', line 29

def evaluate(inside_quotes, only_expression: false)
  if @context.mapping_has_key?(@parameter_name)
    formatted_value = format(@context.mapped_value(@parameter_name), inside_quotes)
    if @context.destination_encoding == :raw && formatted_value.nil?
      only_expression ? '' : "#{@context.parameter_start}#{@parameter_name}#{@context.parameter_end}"
    else
      formatted_value
    end
  else
    if @context.destination_encoding == :raw && only_expression
      ''
    else
      "#{@context.parameter_start}#{@parameter_name}#{@context.parameter_end}"
    end
  end
end

#unknown_parameters(inside_quotes) ⇒ Object



23
24
25
26
27
# File 'lib/parameter_substitution/substitution_expression.rb', line 23

def unknown_parameters(inside_quotes)
  if !@context.mapping_has_key?(@parameter_name) && !(@context.destination_encoding == :json && inside_quotes)
    @parameter_name
  end
end

#validate(_inside_quotes) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/parameter_substitution/substitution_expression.rb', line 14

def validate(_inside_quotes)
  # TODO: - Chase down callers who pass allow_nil = false.  Can we get rid of them?
  if !@context.allow_nil && @context.mapping_has_key?(@parameter_name) && @context.mapped_value(@parameter_name).nil?
    raise ParameterSubstitution::ParseError, "Replacement parameter '#{@parameter_name}' is nil"
  end

  method_calls.each(&:validate)
end