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 =
"3.0.0"

Class Method Summary collapse

Class Method Details

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

Yields:

  • (config)


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

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



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
59
# File 'lib/parameter_substitution.rb', line 29

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: {}, context_overrides: {}) ⇒ Object



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

def find_formatters(string_with_tokens, mapping: {}, context_overrides: {})
  context = build_context(string_with_tokens, mapping, context_overrides)
  parse_expression(context).method_names
end

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



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

def find_tokens(string_with_tokens, mapping: {}, context_overrides: {})
  context = build_context(string_with_tokens, mapping, context_overrides)
  parse_expression(context).substitution_parameter_names
end

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



77
78
79
80
# File 'lib/parameter_substitution.rb', line 77

def find_warnings(string_with_tokens, mapping: {}, context_overrides: {})
  context = build_context(string_with_tokens, mapping, context_overrides)
  parse_expression(context).parameter_and_method_warnings || []
end