Module: Bosh::Template::PropertyHelper

Included in:
EvaluationContext, EvaluationLink, EvaluationLinkInstance, Test::Template
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)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bosh/template/property_helper.rb', line 11

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

  keys.each do |key|
    unless src_ref.is_a?(Hash)
      raise Bosh::Template::InvalidPropertyType,
        "Property '#{name}' expects a hash, but received '#{src_ref.class}'"
    end
    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



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bosh/template/property_helper.rb', line 36

def lookup_property(collection, name)
  return nil if collection.nil?
  keys = name.split(".")
  ref = collection

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

  ref
end

#set_property(dst, name, value) ⇒ Object

Inject property with a given name and value to dst.

Parameters:

  • dst (Hash)

    Property destination

  • name (String)

    Property name (dot-separated)

  • value (Object)

    Property value to be set



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bosh/template/property_helper.rb', line 63

def set_property(dst, name, value)
  keys = name.split('.')
  dst_ref = dst

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

  dst_ref[keys[-1]] = value
end

#sort_property(property) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/bosh/template/property_helper.rb', line 49

def sort_property(property)
  if property.is_a?(Hash)
    property.each do |k, v|
      property[k] = sort_property(v)
    end.sort.to_h
  else
    property
  end
end

#validate_properties_format(properties, name) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bosh/template/property_helper.rb', line 75

def validate_properties_format(properties, name)
  keys = name.split('.')
  properties_ref = properties

  keys.each do |key|
    unless properties_ref.is_a?(Hash)
      raise Bosh::Template::InvalidPropertyType,
            "Property '#{name}' expects a hash, but received '#{properties_ref.class}'"
    end
    properties_ref = properties_ref[key]
    break if properties_ref.nil? # no property with this name is src
  end
end