class Bosh::Template::EvaluationContext

Helper class to evaluate templates. Used by Director, CLI and Agent.

Attributes

index[R]

@return [Integer] Template instance index

name[R]

@return [String] Template name

properties[R]

@return [Hash] Template properties

raw_properties[R]

@return [Hash] Raw template properties (no openstruct)

spec[R]

@return [Hash] Template spec

Public Class Methods

new(spec) click to toggle source

@param [Hash] spec Template spec

# File lib/bosh/template/evaluation_context.rb, line 31
def initialize(spec)
  unless spec.is_a?(Hash)
    raise EvaluationFailed,
          'Invalid spec provided for template evaluation context, ' +
              "Hash expected, #{spec.class} given"
  end

  if spec['job'].is_a?(Hash)
    @name = spec['job']['name']
  else
    @name = nil
  end

  @index = spec['index']
  @spec = openstruct(spec)
  @raw_properties = spec['properties'] || {}
  @properties = openstruct(@raw_properties)

  @links = spec['links'] || {}
end

Public Instance Methods

get_binding() click to toggle source

@return [Binding] Template binding

# File lib/bosh/template/evaluation_context.rb, line 53
def get_binding
  binding.taint
end
if_p(*names) { |*values| ... } click to toggle source

Run a block of code if all given properties are defined @param [Array<String>] names Property names @yield [Object] property values

# File lib/bosh/template/evaluation_context.rb, line 112
def if_p(*names)
  values = names.map do |name|
    value = lookup_property(@raw_properties, name)
    return ActiveElseBlock.new(self) if value.nil?
    value
  end

  yield *values
  InactiveElseBlock.new
end
openstruct(object) click to toggle source

@return [Object] Object representation where all hashes are unrolled

into OpenStruct objects. This exists mostly for backward
compatibility, as it doesn't provide good error reporting.
# File lib/bosh/template/evaluation_context.rb, line 143
def openstruct(object)
  case object
    when Hash
      mapped = object.inject({}) { |h, (k, v)| h[k] = openstruct(v); h }
      OpenStruct.new(mapped)
    when Array
      object.map { |item| openstruct(item) }
    else
      object
  end
end
p(*args) click to toggle source

Property lookup helper

@overload p(name, default_value)

Returns property value or default value if property not set
@param [String] name Property name
@param [Object] default_value Default value
@return [Object] Property value

@overload p(names, default_value)

Returns first property from the list that is set or default value if
none of them are set
@param [Array<String>] names Property names
@param [Object] default_value Default value
@return [Object] Property value

@overload p(names)

Looks up first property from the list that is set, raises an error
if none of them are set.
@param [Array<String>] names Property names
@return [Object] Property value
@raise [Bosh::Common::UnknownProperty]

@overload p(name)

Looks up property and raises an error if it's not set
@param [String] name Property name
@return [Object] Property value
@raise [Bosh::Common::UnknownProperty]
# File lib/bosh/template/evaluation_context.rb, line 84
def p(*args)
  names = Array(args[0])

  names.each do |name|
    result = lookup_property(@raw_properties, name)
    return result unless result.nil?
  end

  return args[1] if args.length == 2
  raise UnknownProperty.new(names)
end