Module: Noventius::Report::Interpolator::InstanceMethods

Defined in:
lib/noventius/report/interpolator.rb

Constant Summary collapse

UNESCAPED_REGEX =
/(<%\w+%>)+/
UNESCAPED_VARIABLE_REGEX =
/(?:<%)(\w+)(?:%>)/
ESCAPED_REGEX =
/({\w+})+/
ESCAPED_VARIABLE_REGEX =
/(?:{)(\w+)(?:})/

Instance Method Summary collapse

Instance Method Details

#interpolate(text) ⇒ String

Interpolate the given text in the context of ‘self`

Interpolated variables will try to call a method on ‘self` for obtaining the value.

Two syntax are supported at the moment.

Let’s say we have a method called foo that return String ‘bar’

foo => ‘bar’ <%foo%> => bar

Parameters:

  • text (String)

    The text to interpolate

Returns:

  • (String)

    The interpolated text



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/noventius/report/interpolator.rb', line 37

def interpolate(text)
  return unless text

  # Interpolate escaped variables
  text.scan(ESCAPED_REGEX).flatten.each do |var|
    var_name = var.match(ESCAPED_VARIABLE_REGEX)[1]
    value = send(var_name.to_sym)

    text.gsub!(var, escape(value))
  end

  # Interpolate unescaped variables
  text.scan(UNESCAPED_REGEX).flatten.each do |var|
    var_name = var.match(UNESCAPED_VARIABLE_REGEX)[1]
    value = send(var_name.to_sym)

    text.gsub!(var, value)
  end

  text
end