Class: MakeMeSpiffy::InputManifest

Inherits:
Object
  • Object
show all
Defined in:
lib/makemespiffy/input_manifest.rb

Defined Under Namespace

Classes: UnknownScopeError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bosh_manifest_yaml) ⇒ InputManifest

Returns a new instance of InputManifest.



14
15
16
# File 'lib/makemespiffy/input_manifest.rb', line 14

def initialize(bosh_manifest_yaml)
  @manifest = bosh_manifest_yaml
end

Instance Attribute Details

#manifestObject (readonly)

Returns the value of attribute manifest.



5
6
7
# File 'lib/makemespiffy/input_manifest.rb', line 5

def manifest
  @manifest
end

Class Method Details

.from_file(manifest_path) ⇒ Object



9
10
11
12
# File 'lib/makemespiffy/input_manifest.rb', line 9

def self.from_file(manifest_path)
  file = YAML.load_file(manifest_path)
  self.new(file)
end

Instance Method Details

#insert_scope_value(meta_scope, value) ⇒ Object

Usage: insert_scope_value(“meta.foo.bar”, 1234) Will add the following into manifest YAML

meta:
  foo:
    bar: 1234


47
48
49
50
51
52
53
54
55
56
# File 'lib/makemespiffy/input_manifest.rb', line 47

def insert_scope_value(meta_scope, value)
  parts = meta_scope.split('.')
  submanifest = manifest
  scoping_parts, key = parts[0..-2], parts[-1]
  for part in scoping_parts
    submanifest[part] ||= {}
    submanifest = submanifest[part]
  end
  submanifest[key] = value
end

#spiffy(extraction_scope, meta_scope) ⇒ Object

Primary method to replace a chunk of manifest with a (( meta.scope ))



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/makemespiffy/input_manifest.rb', line 19

def spiffy(extraction_scope, meta_scope)
  part, *other_parts = extraction_scope.split('.')

  if manifest.is_a?(Array)
    part_value = manifest.find { |item| item["name"] == part }
  else
    part_value = manifest[part]
  end

  if other_parts.size == 0
    if part_value
      manifest[extraction_scope] = "(( #{meta_scope} ))"
    end
    return part_value
  else
    unless part_value
      raise UnknownScopeError, extraction_scope
    end
    inner_manifest = InputManifest.new(part_value)
    return inner_manifest.spiffy(other_parts.join("."), meta_scope)
  end
end