Class: MotherBrain::Manifest
- Inherits:
-
Hash
- Object
- Hash
- MotherBrain::Manifest
show all
- Defined in:
- lib/mb/manifest.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = Hash.new) ⇒ Manifest
Returns a new instance of Manifest.
38
39
40
41
42
|
# File 'lib/mb/manifest.rb', line 38
def initialize(attributes = Hash.new)
if attributes && attributes.any?
from_hash(attributes)
end
end
|
Instance Attribute Details
#path ⇒ Object
35
36
37
|
# File 'lib/mb/manifest.rb', line 35
def path
@path
end
|
Class Method Details
.from_file(path) ⇒ Manifest
9
10
11
12
13
14
15
16
17
|
# File 'lib/mb/manifest.rb', line 9
def from_file(path)
path = File.expand_path(path.to_s)
data = File.read(path)
obj = new.from_json(data)
obj.path = path
obj
rescue Errno::ENOENT
raise ManifestNotFound, "No manifest found at: '#{path}'"
end
|
.from_hash(data) ⇒ Manifest
29
30
31
|
# File 'lib/mb/manifest.rb', line 29
def from_hash(data)
new.from_hash(data)
end
|
.from_json(data) ⇒ Manifest
22
23
24
|
# File 'lib/mb/manifest.rb', line 22
def from_json(data)
new.from_json(data)
end
|
Instance Method Details
#from_hash(hash) ⇒ Manifest
60
61
62
63
64
|
# File 'lib/mb/manifest.rb', line 60
def from_hash(hash)
mass_assign(hash)
self
end
|
#from_json(json, options = {}) ⇒ Manifest
51
52
53
54
55
|
# File 'lib/mb/manifest.rb', line 51
def from_json(json, options = {})
from_hash(MultiJson.decode(json, options))
rescue MultiJson::DecodeError => error
raise InvalidManifest, error
end
|
#node_count ⇒ Integer
Returns the number of nodes expected to be created by this manifest regardless of type
99
100
101
102
103
|
# File 'lib/mb/manifest.rb', line 99
def node_count
node_groups.reduce(0) { |total, node|
total + (node[:count] || 1)
}
end
|
#node_groups ⇒ Array
87
88
89
|
# File 'lib/mb/manifest.rb', line 87
def node_groups
self[:nodes] || []
end
|
#options ⇒ Hash
92
93
94
|
# File 'lib/mb/manifest.rb', line 92
def options
self[:options] || {}
end
|
#save(path = nil) ⇒ Manifest
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/mb/manifest.rb', line 71
def save(path = nil)
self.path = path || self.path
unless path.present?
raise InternalError, "Cannot save manifest without a destination. Set the 'path' attribute on your object."
end
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w+') do |file|
file.write(MultiJson.dump(self, pretty: true))
end
self
end
|