Module: Bosh::Template::PropertyHelper

Included in:
EvaluationContext
Defined in:
lib/bosh/template/property_helper.rb

Instance Method Summary collapse

Instance Method Details

#copy_property(dst, src, name, default = nil) ⇒ Object

Copies property with a given name from src to dst.

Parameters:

  • dst (Hash)

    Property destination

  • src (Hash)

    Property source

  • name (String)

    Property name (dot-separated)

  • default (Object) (defaults to: nil)

    Default value (if property is not in src)



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bosh/template/property_helper.rb', line 9

def copy_property(dst, src, name, default = nil)
  keys = name.split(".")
  src_ref = src
  dst_ref = dst

  keys.each do |key|
    src_ref = src_ref[key]
    break if src_ref.nil? # no property with this name is src
  end

  keys[0..-2].each do |key|
    dst_ref[key] ||= {}
    dst_ref = dst_ref[key]
  end

  dst_ref[keys[-1]] ||= {}
  dst_ref[keys[-1]] = src_ref.nil? ? default : src_ref
end

#lookup_property(collection, name) ⇒ Object

Parameters:

  • collection (Hash)

    Property collection

  • name (String)

    Dot-separated property name



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bosh/template/property_helper.rb', line 30

def lookup_property(collection, name)
  keys = name.split(".")
  ref = collection

  keys.each do |key|
    ref = ref[key]
    return nil if ref.nil?
  end

  ref
end