class Bosh::Template::Test::Template

Public Class Methods

new(job_spec_hash, template_path) click to toggle source
# File lib/bosh/template/test/template.rb, line 9
def initialize(job_spec_hash, template_path)
  @job_spec_hash = job_spec_hash
  @template_path = template_path
end

Public Instance Methods

render(manifest_properties_hash, spec: InstanceSpec.new, consumes: []) click to toggle source
# File lib/bosh/template/test/template.rb, line 14
def render(manifest_properties_hash, spec: InstanceSpec.new, consumes: [])
  spec_hash = {}
  spec_hash['properties'] = hash_with_defaults(manifest_properties_hash)
  sanitized_hash_with_spec = spec_hash.merge(spec.to_h)
  sanitized_hash_with_spec['links'] = links_hash(consumes)

  binding = Bosh::Template::EvaluationContext.new(sanitized_hash_with_spec, nil).get_binding
  raise "No such file at #{@template_path}" unless File.exist?(@template_path)
  ERB.new(File.read(@template_path), nil, '-').result(binding)
end

Private Instance Methods

hash_with_defaults(manifest_properties_hash) click to toggle source
# File lib/bosh/template/test/template.rb, line 27
def hash_with_defaults(manifest_properties_hash)
  hash_properties = {}
  spec_properties = @job_spec_hash['properties']

  spec_properties.each_pair do |dotted_spec_key, property_def|
    property_val = lookup_property(manifest_properties_hash, dotted_spec_key)
    if property_val.nil? && !property_def['default'].nil?
      property_val = property_def['default']
    end
    insert_property(hash_properties, dotted_spec_key, property_val)
  end

  hash_properties
end
insert_property(nested_hash, dotted_key, value) click to toggle source
# File lib/bosh/template/test/template.rb, line 62
def insert_property(nested_hash, dotted_key, value)
  property_segments = dotted_key.split('.')
  current_level = nested_hash

  property_segments.each_with_index do |property_segment, i|
    if i == property_segments.count - 1
      current_level[property_segment] = value
    else
      current_level[property_segment] ||= {}
      current_level = current_level[property_segment]
    end
  end
end