Class: ParameterSubstitution

Inherits:
Object
  • Object
show all
Defined in:
lib/parameter_substitution.rb,
lib/parameter_substitution/parser.rb,
lib/parameter_substitution/context.rb,
lib/parameter_substitution/encoder.rb,
lib/parameter_substitution/version.rb,
lib/parameter_substitution/transform.rb,
lib/parameter_substitution/expression.rb,
lib/parameter_substitution/parse_error.rb,
lib/parameter_substitution/configuration.rb,
lib/parameter_substitution/formatters/base.rb,
lib/parameter_substitution/text_expression.rb,
lib/parameter_substitution/formatters/manager.rb,
lib/parameter_substitution/method_call_expression.rb,
lib/parameter_substitution/substitution_expression.rb

Defined Under Namespace

Modules: Formatters Classes: Configuration, Context, Encoder, Expression, MethodCallExpression, ParseError, Parser, SubstitutionError, SubstitutionExpression, TextExpression, Transform

Constant Summary collapse

VERSION =
"1.2.0"

Class Method Summary collapse

Class Method Details

.configure {|config| ... } ⇒ Object

Yields:

  • (config)


60
61
62
63
64
# File 'lib/parameter_substitution.rb', line 60

def configure
  config = ParameterSubstitution::Configuration.new
  yield(config)
  ParameterSubstitution.config = config
end

.evaluate(input:, mapping:, required_parameters: [], parameter_start: "<", parameter_end: ">", destination_encoding: :text, allow_unknown_replacement_parameters: false, allow_nil: false, allow_unmatched_parameter_end: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/parameter_substitution.rb', line 28

def evaluate(
  input:,
  mapping:,
  required_parameters: [],
  parameter_start: "<",
  parameter_end: ">",
  destination_encoding: :text,
  allow_unknown_replacement_parameters: false,
  allow_nil: false,
  allow_unmatched_parameter_end: false
)
  context = ParameterSubstitution::Context.new(
    input: input,
    mapping: mapping,
    required_parameters: required_parameters,
    parameter_start: parameter_start,
    parameter_end: parameter_end,
    destination_encoding: destination_encoding,
    allow_unknown_replacement_parameters: allow_unknown_replacement_parameters,
    allow_nil: allow_nil,
    allow_unmatched_parameter_end: allow_unmatched_parameter_end
  )

  expression = parse_expression(context)
  expression.validate
  [expression.evaluate, expression.warnings, expression.parameter_and_method_warnings]
rescue ParameterSubstitution::ParseError => ex
  [context.input, ex.message, expression&.parameter_and_method_warnings]
rescue => ex
  raise SubstitutionError, "Error occurred while parameter substitution: #{ex.message}"
end

.find_formatters(string_with_tokens, mapping: {}) ⇒ Object



70
71
72
# File 'lib/parameter_substitution.rb', line 70

def find_formatters(string_with_tokens, mapping: {})
  parse_expression(context_from_string(string_with_tokens, mapping)).method_names
end

.find_tokens(string_with_tokens, mapping: {}) ⇒ Object



66
67
68
# File 'lib/parameter_substitution.rb', line 66

def find_tokens(string_with_tokens, mapping: {})
  parse_expression(context_from_string(string_with_tokens, mapping)).substitution_parameter_names
end

.find_warnings(string_with_tokens, mapping: {}) ⇒ Object



74
75
76
# File 'lib/parameter_substitution.rb', line 74

def find_warnings(string_with_tokens, mapping: {})
  parse_expression(context_from_string(string_with_tokens, mapping)).parameter_and_method_warnings || []
end