Class: Machinery::Manifest

Inherits:
Object show all
Defined in:
lib/manifest.rb

Overview

The Manifest class takes care of handling the JSON representation of system descriptions. It loads and parses the JSON into a hash and validates it against the current schema.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, json, path = nil) ⇒ Manifest

Returns a new instance of Manifest.



35
36
37
38
39
40
41
# File 'lib/manifest.rb', line 35

def initialize(name, json, path = nil)
  @name = name
  @path = path
  @json = json

  parse
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



22
23
24
# File 'lib/manifest.rb', line 22

def hash
  @hash
end

#jsonObject

Returns the value of attribute json.



22
23
24
# File 'lib/manifest.rb', line 22

def json
  @json
end

#nameObject

Returns the value of attribute name.



22
23
24
# File 'lib/manifest.rb', line 22

def name
  @name
end

#pathObject

Returns the value of attribute path.



22
23
24
# File 'lib/manifest.rb', line 22

def path
  @path
end

Class Method Details

.load(name, path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/manifest.rb', line 24

def self.load(name, path)
  unless File.exist?(path)
    raise Machinery::Errors::SystemDescriptionNotFound.new(
      "Couldn't find a system description with the name '#{name}'."
    )
  end

  json = File.read(path)
  Machinery::Manifest.new(name, json, path)
end

Instance Method Details

#to_hashObject



62
63
64
# File 'lib/manifest.rb', line 62

def to_hash
  @hash
end

#validateObject



43
44
45
46
47
48
49
50
51
# File 'lib/manifest.rb', line 43

def validate
  return unless compatible_json?

  errors = Machinery::JsonValidator.new(@hash).validate
  unless errors.empty?
    Machinery::Ui.warn("Warning: System Description validation errors:")
    Machinery::Ui.warn(errors.join("\n"))
  end
end

#validate!Object



53
54
55
56
57
58
59
60
# File 'lib/manifest.rb', line 53

def validate!
  return unless compatible_json?

  errors = Machinery::JsonValidator.new(@hash).validate
  unless errors.empty?
    raise Machinery::Errors::SystemDescriptionValidationFailed.new(errors)
  end
end