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, BackCompatOpenStruct, 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_link(name) {|Object| ... } ⇒ Object
Run a block of code if the link given exists.
-
#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
-
#p(*args) ⇒ Object
Property lookup helper.
Methods included from PropertyHelper
#copy_property, #lookup_property
Constructor Details
#initialize(spec) ⇒ EvaluationContext
Returns a new instance of EvaluationContext.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# 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, BackCompatOpenStruct) @raw_properties = spec['properties'] || {} @properties = openstruct(@raw_properties) @links = spec['links'] || {} end |
Instance Attribute Details
#index ⇒ Integer (readonly)
Returns Template instance index.
19 20 21 |
# File 'lib/bosh/template/evaluation_context.rb', line 19 def index @index end |
#name ⇒ String (readonly)
Returns Template name.
16 17 18 |
# File 'lib/bosh/template/evaluation_context.rb', line 16 def name @name end |
#properties ⇒ Hash (readonly)
Returns Template properties.
22 23 24 |
# File 'lib/bosh/template/evaluation_context.rb', line 22 def properties @properties end |
#raw_properties ⇒ Hash (readonly)
Returns Raw template properties (no openstruct).
25 26 27 |
# File 'lib/bosh/template/evaluation_context.rb', line 25 def raw_properties @raw_properties end |
#spec ⇒ Hash (readonly)
Returns Template spec.
28 29 30 |
# File 'lib/bosh/template/evaluation_context.rb', line 28 def spec @spec end |
Instance Method Details
#get_binding ⇒ Binding
Returns Template binding.
53 54 55 |
# File 'lib/bosh/template/evaluation_context.rb', line 53 def get_binding binding.taint end |
#if_link(name) {|Object| ... } ⇒ Object
Run a block of code if the link given exists
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/bosh/template/evaluation_context.rb', line 126 def if_link(name) link_found = lookup_property(@links, name) if link_found.nil? || !link_found.has_key?("instances") return ActiveElseBlock.new(self) else instance_array = link_found["instances"].map do |link_spec| EvaluationLinkInstance.new(link_spec["name"], link_spec["index"], link_spec["id"], link_spec["az"], link_spec["address"], link_spec["properties"], link_spec["bootstrap"]) end yield EvaluationLink.new(instance_array, link_found["properties"]) InactiveElseBlock.new end end |
#if_p(*names) {|Object| ... } ⇒ Object
Run a block of code if all given properties are defined
112 113 114 115 116 117 118 119 120 121 |
# 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 |
#link(name) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/bosh/template/evaluation_context.rb', line 96 def link(name) result = lookup_property(@links, name) raise UnknownLink.new(name) if result.nil? if result.has_key?("instances") instance_array = result["instances"].map do |link_spec| EvaluationLinkInstance.new(link_spec["name"], link_spec["index"], link_spec["id"], link_spec["az"], link_spec["address"], link_spec["properties"], link_spec["bootstrap"]) end return EvaluationLink.new(instance_array, result["properties"]) end raise UnknownLink.new(name) end |
#p(name, default_value) ⇒ Object #p(names, default_value) ⇒ Object #p(names) ⇒ Object #p(name) ⇒ Object
Property lookup helper
84 85 86 87 88 89 90 91 92 93 94 |
# 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 |