Class: ParameterSubstitution::Expression
- Inherits:
-
Object
- Object
- ParameterSubstitution::Expression
- Defined in:
- lib/parameter_substitution/expression.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#expression_list ⇒ Object
readonly
Returns the value of attribute expression_list.
Instance Method Summary collapse
- #evaluate ⇒ Object
-
#initialize(expression_list, context) ⇒ Expression
constructor
A new instance of Expression.
- #method_names ⇒ Object
- #methods_used_by_expression(expression) ⇒ Object
- #parameter_and_method_warnings ⇒ Object
- #substitution_parameter_names ⇒ Object
- #validate ⇒ Object
- #warnings ⇒ Object
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
#context ⇒ Object (readonly)
Returns the value of attribute context.
7 8 9 |
# File 'lib/parameter_substitution/expression.rb', line 7 def context @context end |
#expression_list ⇒ Object (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
#evaluate ⇒ Object
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_names ⇒ Object
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_warnings ⇒ Object
36 37 38 39 |
# File 'lib/parameter_substitution/expression.rb', line 36 def parameter_and_method_warnings = + .empty? ? nil : .uniq end |
#substitution_parameter_names ⇒ Object
41 42 43 |
# File 'lib/parameter_substitution/expression.rb', line 41 def substitution_parameter_names @expression_list.map_compact(&:parameter_name) end |
#validate ⇒ Object
14 15 16 17 18 |
# File 'lib/parameter_substitution/expression.rb', line 14 def validate validate_required_parameters validate_unknown_parameters validate_expressions end |
#warnings ⇒ Object
32 33 34 |
# File 'lib/parameter_substitution/expression.rb', line 32 def warnings end |