Class: CloudFormula::Template
- Inherits:
-
Object
- Object
- CloudFormula::Template
- Defined in:
- lib/cloudformula/template.rb
Overview
Defines a CloudFormula/CloudFormation template
Instance Attribute Summary collapse
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
Instance Method Summary collapse
-
#aws_options(override_options = {}, parameters = {}) ⇒ Object
Returns the merged options Hash for use by AWS::CloudFormation::StackCollection#create and AWS::CloudFormation::Stack#update.
-
#errors ⇒ Object
Similar to Active Record Validations errors[] object.
-
#generate ⇒ String
Evaluates and returns the @source template populated with values from @parameters.
-
#generate_hash ⇒ Hash
Evalutes and returns the @source template populated with values from @parameters.
-
#initialize(source = '', parameters = {}) ⇒ Template
constructor
A new instance of Template.
- #invalid? ⇒ Boolean
-
#raw(value) ⇒ String
Causes the given string value to be output without escape characters.
-
#render(source, parameters = {}) ⇒ String
The result of generating the template.
-
#stack_options ⇒ Object
Returns the CloudFormation stack options defined by the template.
- #valid? ⇒ Boolean
-
#validations ⇒ Object
Returns the validations defined by the template.
Constructor Details
#initialize(source = '', parameters = {}) ⇒ Template
Returns a new instance of Template.
10 11 12 13 |
# File 'lib/cloudformula/template.rb', line 10 def initialize(source = '', parameters = {}) @source = source @parameters = parameters end |
Instance Attribute Details
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
6 7 8 |
# File 'lib/cloudformula/template.rb', line 6 def parameters @parameters end |
Instance Method Details
#aws_options(override_options = {}, parameters = {}) ⇒ Object
Returns the merged options Hash for use by AWS::CloudFormation::StackCollection#create and AWS::CloudFormation::Stack#update
65 66 67 |
# File 'lib/cloudformula/template.rb', line 65 def ( = {}, parameters = {}) .merge(.merge({ :parameters => parameters })) end |
#errors ⇒ Object
Similar to Active Record Validations errors[] object
71 72 73 |
# File 'lib/cloudformula/template.rb', line 71 def errors validate end |
#generate ⇒ String
Evaluates and returns the @source template populated with values from @parameters
17 18 19 20 21 |
# File 'lib/cloudformula/template.rb', line 17 def generate init_parameters @parameters raise "Parameter validation failed.\n\nErrors:\n #{errors.join("\n")}" if invalid? JsonErb.new(File.read(@source)).result(binding) end |
#generate_hash ⇒ Hash
Evalutes and returns the @source template populated with values from @parameters
25 26 27 |
# File 'lib/cloudformula/template.rb', line 25 def generate_hash JSON.parse(generate) end |
#invalid? ⇒ Boolean
79 80 81 |
# File 'lib/cloudformula/template.rb', line 79 def invalid? !valid? end |
#raw(value) ⇒ String
Causes the given string value to be output without escape characters
32 33 34 35 |
# File 'lib/cloudformula/template.rb', line 32 def raw(value) value.instance_variable_set :@cloudformula_raw, true value end |
#render(source, parameters = {}) ⇒ String
Returns The result of generating the template.
40 41 42 43 44 45 46 |
# File 'lib/cloudformula/template.rb', line 40 def render(source, parameters = {}) raw CloudFormula::Template.new(source, parameters).generate rescue Errno::ENOENT # retry the path, but relative to its parent template relative_to_parent = Pathname.new(@source).parent + source raw CloudFormula::Template.new(relative_to_parent, parameters).generate end |
#stack_options ⇒ Object
Returns the CloudFormation stack options defined by the template.
49 50 51 52 |
# File 'lib/cloudformula/template.rb', line 49 def init_parameters @parameters JsonErb.new(File.read(@source), 0).(binding) rescue {} end |
#valid? ⇒ Boolean
75 76 77 |
# File 'lib/cloudformula/template.rb', line 75 def valid? errors.length == 0 end |
#validations ⇒ Object
Returns the validations defined by the template.
55 56 57 58 |
# File 'lib/cloudformula/template.rb', line 55 def validations init_parameters @parameters JsonErb.new(File.read(@source), 0).validations(binding) rescue {} end |