Class: StackMaster::CloudFormationInterpolatingEruby

Inherits:
Erubis::Eruby
  • Object
show all
Includes:
Erubis::ArrayEnhancer
Defined in:
lib/stack_master/cloudformation_interpolating_eruby.rb

Overview

This class is a modified version of ‘Erubis::Eruby`. It allows using `<%= %>` ERB expressions to interpolate values into a source string. We use this capability to enrich user data scripts with data and parameters pulled from the AWS CloudFormation service. The evaluation produces an array of objects ready for use in a CloudFormation `Fn::Join` intrinsic function.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.evaluate_file(source_path, context = Erubis::Context.new) ⇒ Object

Load a template from a file at the specified path and evaluate it.



15
16
17
18
19
20
# File 'lib/stack_master/cloudformation_interpolating_eruby.rb', line 15

def self.evaluate_file(source_path, context = Erubis::Context.new)
  template_contents = File.read(source_path)
  eruby = new(template_contents)
  eruby.filename = source_path
  eruby.evaluate(context)
end

Instance Method Details

#add_expr(src, code, indicator) ⇒ Object

See Also:

  • Erubis::Eruby#add_expr


36
37
38
39
40
41
42
# File 'lib/stack_master/cloudformation_interpolating_eruby.rb', line 36

def add_expr(src, code, indicator)
  if indicator == '='
    src << " #{@bufvar} << (" << code << ');'
  else
    super
  end
end

#evaluate(_context = Erubis::Context.new) ⇒ Array

Returns The result of evaluating the source: an array of strings from the source intermindled with Hash objects from the ERB expressions. To be included in a CloudFormation template, this value needs to be used in a CloudFormation ‘Fn::Join` intrinsic function.

Examples:

CloudFormationInterpolatingEruby.new("my_variable=<%= { 'Ref' => 'Param1' } %>;").evaluate
  #=> ['my_variable=', { 'Ref' => 'Param1' }, ';']

Returns:

  • (Array)

    The result of evaluating the source: an array of strings from the source intermindled with Hash objects from the ERB expressions. To be included in a CloudFormation template, this value needs to be used in a CloudFormation ‘Fn::Join` intrinsic function.

See Also:

  • Erubis::Eruby#evaluate


31
32
33
# File 'lib/stack_master/cloudformation_interpolating_eruby.rb', line 31

def evaluate(_context = Erubis::Context.new)
  format_lines_for_cloudformation(super)
end