Class: CloudCompose::Template
- Inherits:
-
Object
- Object
- CloudCompose::Template
- Defined in:
- lib/cloud_compose/template.rb
Defined Under Namespace
Constant Summary collapse
- IMPORT_MERGE_KEYS =
%w[ Metadata Parameters Mappings Conditions Transform Resources Outputs ].freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #checksums ⇒ Object
- #description(depth) ⇒ Object
- #imported ⇒ Object
-
#initialize(path, pwd) ⇒ Template
constructor
A new instance of Template.
- #parameters ⇒ Object
- #required_parameters ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(path, pwd) ⇒ Template
Returns a new instance of Template.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cloud_compose/template.rb', line 31 def initialize(path, pwd) @pwd = pwd @file_name = File.basename(path) @root = Pathname.new(File.dirname(path)) content = File.read(path) @checksum = Digest::SHA256.hexdigest(content) parts = content.split(/^---/, 3) @config = CloudCompose::Config.new(parts[1], @root) @content = parts[2] || '' end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
29 30 31 |
# File 'lib/cloud_compose/template.rb', line 29 def config @config end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
29 30 31 |
# File 'lib/cloud_compose/template.rb', line 29 def content @content end |
#file_name ⇒ Object (readonly)
Returns the value of attribute file_name.
29 30 31 |
# File 'lib/cloud_compose/template.rb', line 29 def file_name @file_name end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
29 30 31 |
# File 'lib/cloud_compose/template.rb', line 29 def root @root end |
Instance Method Details
#checksums ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/cloud_compose/template.rb', line 96 def checksums return @checksums if @checksums checksums = {} checksums["#{working_path}/#{file_name}"] = @checksum imported.each do |imp| imp.template.checksums.each do |key, value| checksums[key] = value end end @checksums = checksums @checksums end |
#description(depth) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cloud_compose/template.rb', line 85 def description(depth) callouts = depth.zero? ? '├─' : '↳' leading = depth.zero? ? '' : '|' padding = ' ' * depth joined = imported.map { |i| i.template.description(depth + 2) } [ "# #{leading}#{padding}#{callouts} #{working_path}/#{file_name}", *joined ].join("\n") end |
#imported ⇒ Object
72 73 74 75 |
# File 'lib/cloud_compose/template.rb', line 72 def imported preload_imports! @imported end |
#parameters ⇒ Object
77 78 79 |
# File 'lib/cloud_compose/template.rb', line 77 def parameters @config.parameters end |
#required_parameters ⇒ Object
81 82 83 |
# File 'lib/cloud_compose/template.rb', line 81 def required_parameters @config.required_parameters end |
#to_h ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/cloud_compose/template.rb', line 42 def to_h template_body = create_hash(parameters) imported.each do |imp| formatted_imp = imp.template.send(:create_hash, imp.context) merge_imported(template_body, formatted_imp, imp.name) end template_body.keys.each do |key| template_body.delete(key) if template_body[key].empty? end template_body end |
#to_s ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/cloud_compose/template.rb', line 56 def to_s yaml = CloudCompose::Parser.dump_yaml(to_h) <<~EOF # This Template was generated on #{Time.now.utc.iso8601} by CloudCompose #{CloudCompose::VERSION} # # Structure #{description(0)} # # Integrity #{checksum_print} # #{yaml} EOF end |