Class: Cloudruler::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudruler/template.rb

Defined Under Namespace

Classes: EvalContext

Constant Summary collapse

DEFAULT_FORMAT_VERSION =
'2010-09-09'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner, path) ⇒ Template

Returns a new instance of Template.



7
8
9
10
11
12
13
# File 'lib/cloudruler/template.rb', line 7

def initialize(runner, path)
  @runner = runner
  @path = path
  @rulers = []
  @format_version = DEFAULT_FORMAT_VERSION
  @description = ''
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/cloudruler/template.rb', line 5

def description
  @description
end

#format_versionObject

Returns the value of attribute format_version.



5
6
7
# File 'lib/cloudruler/template.rb', line 5

def format_version
  @format_version
end

#rulersObject

Returns the value of attribute rulers.



5
6
7
# File 'lib/cloudruler/template.rb', line 5

def rulers
  @rulers
end

Instance Method Details

#bodyObject



40
41
42
# File 'lib/cloudruler/template.rb', line 40

def body
  @body ||= File.read(@path)
end

#dumpObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cloudruler/template.rb', line 22

def dump
  result = {
    'AWSTemplateFormatVersion' => @format_version,
    'Description' => @description,
  }
  result['Metadata'] = @rulers.map(&:metadata).compact.map(&:content)
  result['Parameters'] = @rulers.map(&:parameters).compact.map(&:content)
  result['Mappings'] = @rulers.map(&:mappings).compact.map(&:content)
  result['Conditions'] = @rulers.map(&:conditions).compact.map(&:content)
  result['Transform'] = @rulers.map(&:transform).compact.map(&:content)
  result['Resources'] = @rulers.map(&:resources).compact.map(&:content)
  result['Outputs'] = @rulers.map(&:outputs).compact.map(&:content)
  result = result.reject { |_, v| v.empty? }.each_with_object({}) do |(k, v), obj|
    v.respond_to?(:reduce) ? obj[k] = v.reduce(&:merge) : obj[k] = v
  end
  result.to_yaml
end

#resolveObject



15
16
17
18
19
20
# File 'lib/cloudruler/template.rb', line 15

def resolve
  @rulers = []
  context = EvalContext.new(self)
  context.instance_eval(body)
  self
end

#resolve_ruler(ruler_name, options, &block) ⇒ Object



44
45
46
47
# File 'lib/cloudruler/template.rb', line 44

def resolve_ruler(ruler_name, options, &block)
  ruler = @runner.find_ruler(ruler_name)
  ruler.resolve(options, &block)
end