Class: Bosh::Template::EvaluationContext
- Inherits:
-
Object
- Object
- Bosh::Template::EvaluationContext
- Includes:
- PropertyHelper
- Defined in:
- lib/bosh/template/evaluation_context.rb
Overview
Helper class to evaluate templates. Used by Director, CLI and Agent.
Defined Under Namespace
Classes: ActiveElseBlock, InactiveElseBlock
Instance Attribute Summary collapse
-
#index ⇒ Integer
readonly
Template instance index.
-
#name ⇒ String
readonly
Template name.
-
#properties ⇒ Hash
readonly
Template properties.
-
#raw_properties ⇒ Hash
readonly
Raw template properties (no openstruct).
-
#spec ⇒ Hash
readonly
Template spec.
Instance Method Summary collapse
-
#get_binding ⇒ Binding
Template binding.
-
#if_p(*names) {|Object| ... } ⇒ Object
Run a block of code if all given properties are defined.
-
#initialize(spec) ⇒ EvaluationContext
constructor
A new instance of EvaluationContext.
- #link(name) ⇒ Object
-
#openstruct(object) ⇒ Object
Object representation where all hashes are unrolled into OpenStruct objects.
-
#p(*args) ⇒ Object
Property lookup helper.
Methods included from PropertyHelper
#copy_property, #lookup_property
Constructor Details
#initialize(spec) ⇒ EvaluationContext
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bosh/template/evaluation_context.rb', line 29 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 |
Instance Attribute Details
#index ⇒ Integer (readonly)
17 18 19 |
# File 'lib/bosh/template/evaluation_context.rb', line 17 def index @index end |
#name ⇒ String (readonly)
14 15 16 |
# File 'lib/bosh/template/evaluation_context.rb', line 14 def name @name end |
#properties ⇒ Hash (readonly)
20 21 22 |
# File 'lib/bosh/template/evaluation_context.rb', line 20 def properties @properties end |
#raw_properties ⇒ Hash (readonly)
23 24 25 |
# File 'lib/bosh/template/evaluation_context.rb', line 23 def raw_properties @raw_properties end |
#spec ⇒ Hash (readonly)
26 27 28 |
# File 'lib/bosh/template/evaluation_context.rb', line 26 def spec @spec end |
Instance Method Details
#get_binding ⇒ Binding
51 52 53 |
# File 'lib/bosh/template/evaluation_context.rb', line 51 def get_binding binding.taint end |
#if_p(*names) {|Object| ... } ⇒ Object
Run a block of code if all given properties are defined
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/bosh/template/evaluation_context.rb', line 104 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 |
#link(name) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/bosh/template/evaluation_context.rb', line 94 def link(name) result = lookup_property(@links, name) return result unless result.nil? raise UnknownLink.new(name) end |
#openstruct(object) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/bosh/template/evaluation_context.rb', line 118 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(name, default_value) ⇒ Object #p(names, default_value) ⇒ Object #p(names) ⇒ Object #p(name) ⇒ Object
Property lookup helper
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/bosh/template/evaluation_context.rb', line 82 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 |