class Spirit::Manifest

Manifest described in YAML. Values in this hash should not be trusted, since they are provided by the user.

Constants

TYPES

Expected types.

Public Class Methods

load(source) click to toggle source

Load configuration from given string. @param [String] source @return [Manifest] manifest

# File lib/spirit/manifest.rb, line 32
def self.load(source)
  new ::YAML.load source
rescue ::Psych::SyntaxError => e
  raise ManifestError, 'Unexpected syntax error - ' + e.message
end
load_file(path) click to toggle source

Load configuration from given yaml file. @param [String] path @return [Manifest] manifest

# File lib/spirit/manifest.rb, line 41
def self.load_file(path)
  File.open(path, 'r:utf-8') { |f| new ::YAML.load f.read }
rescue ::Psych::SyntaxError => e
  raise ManifestError, 'Unexpected syntax error - ' + e.message
end

Private Class Methods

new(hash) click to toggle source

Creates a new manifest from the given source.

Calls superclass method
# File lib/spirit/manifest.rb, line 21
def initialize(hash)
  super nil
  hash ||= {}
  bad_file(hash.class) unless hash.is_a? Hash
  merge! hash.symbolize_keys
  check_types
end

Private Instance Methods

bad_file(type) click to toggle source
# File lib/spirit/manifest.rb, line 62
    def bad_file(type)
      raise ManifestError, <<-eos.squish
        The manifest file should contain a dictionary, but a #{type.name} was
        found.
      eos
    end
bad_type(key, expected, actual, opts={}) click to toggle source
# File lib/spirit/manifest.rb, line 69
    def bad_type(key, expected, actual, opts={})
      verb = opts[:enum] ? 'contain' : 'be'
      raise ManifestError, <<-eos.squish
        The #{key} option in the manifest file should #{verb} a #{expected.class.name}
        instead of a #{actual.class.name}.
      eos
    end
check_types(key='root', expected=TYPES, actual=self, opts={}) click to toggle source

Checks that the given hash has the valid types for each value, if they exist. @raise [ManifestError] if a bad type is encountered.

# File lib/spirit/manifest.rb, line 54
def check_types(key='root', expected=TYPES, actual=self, opts={})
  bad_type(key, expected, actual, opts) unless actual.is_a? expected.class
  case actual
  when Hash then actual.each { |k, v| check_types(k, expected[k], v) }
  when Enumerable then actual.each { |v| check_types(key, expected.first, v, enum: true) }
  end
end