Method: CloudFormationDSL::Helpers#interpolate

Defined in:
lib/cloudformation-dsl/helpers.rb

#interpolate(string, locals = {}) ⇒ Object

Interpolates a string like “NAME={ref(‘Service’)}” and returns a CloudFormation “Fn::Join” operation to collect the results. Anything between and } is interpreted as a Ruby expression and eval’d. This is especially useful with Ruby “here” documents. Local variables may also be exposed to the string via the ‘locals` hash.



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cloudformation-dsl/helpers.rb', line 100

def interpolate(string, locals={})
  list = []
  while string.length > 0
    head, match, string = string.partition(/\{\{.*?\}\}/)
    list << head if head.length > 0
    list << eval(match[2..-3], nil, 'interpolated string') if match.length > 0
  end

  # Split out strings in an array by newline, for visibility
  list = list.flat_map {|value| value.is_a?(String) ? value.lines.to_a : value }
  join('', *list)
end