class MotherBrain::Manifest

Attributes

path[RW]

return [String]

Public Class Methods

from_file(path) click to toggle source

@param [#to_s] path

@raise [ManifestNotFound] if the manifest file is not found

@return [Manifest]

# 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) click to toggle source

@param [Hash] data

@return [Manifest]

# File lib/mb/manifest.rb, line 29
def from_hash(data)
  new.from_hash(data)
end
from_json(data) click to toggle source

@param [#to_s] data

@return [Manifest]

# File lib/mb/manifest.rb, line 22
def from_json(data)
  new.from_json(data)
end
new(attributes = Hash.new) click to toggle source

@param [Hash] attributes (Hash.new)

# File lib/mb/manifest.rb, line 38
def initialize(attributes = Hash.new)
  if attributes && attributes.any?
    from_hash(attributes)
  end
end

Public Instance Methods

from_hash(hash) click to toggle source

@param [Hash] hash

@return [Manifest]

# File lib/mb/manifest.rb, line 60
def from_hash(hash)
  mass_assign(hash)

  self
end
from_json(json, options = {}) click to toggle source

@param [String] json @param [Hash] options

@see MultiJson.decode

@raise [InvalidManifest] if the given string is not valid JSON

@return [Manifest]

# 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() click to toggle source

Returns the number of nodes expected to be created by this manifest regardless of type

@return [Integer]

# File lib/mb/manifest.rb, line 99
def node_count
  node_groups.reduce(0) { |total, node|
    total + (node[:count] || 1)
  }
end
node_groups() click to toggle source

@return [Array]

# File lib/mb/manifest.rb, line 87
def node_groups
  self[:nodes] || []
end
options() click to toggle source

@return [Hash]

# File lib/mb/manifest.rb, line 92
def options
  self[:options] || {}
end
save(path = nil) click to toggle source

@param [String] path

@raise [MB::InternalError] if the path attribute is nil or an empty string

@return [Manifest]

# 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

Private Instance Methods

mass_assign(hash) click to toggle source

Assign the key value pairs of the given hash to self

@param [Hash] hash

# File lib/mb/manifest.rb, line 110
def mass_assign(hash)
  hash.each_pair do |key, value|
    self[key] = value
  end

  deep_symbolize_keys!

  each do |key, value|
    if value.is_a?(Array)
      value.each do |object|
        if object.respond_to?(:deep_symbolize_keys!)
          object.deep_symbolize_keys!
        end
      end
    end
  end
end