Class: RailsDrips::DefinitionLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_drips/definition_loader.rb

Class Method Summary collapse

Class Method Details

.load_file(path) ⇒ Object



7
8
9
10
11
# File 'lib/rails_drips/definition_loader.rb', line 7

def self.load_file(path)
  load_string(File.read(path), source: path.to_s)
rescue Errno::ENOENT => error
  raise DefinitionInvalid, [error.message]
end

.load_string(yaml, source: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rails_drips/definition_loader.rb', line 13

def self.load_string(yaml, source: nil)
  raw = YAML.safe_load(yaml, permitted_classes: [], permitted_symbols: [], aliases: false)
  definition = deep_stringify(raw)
  normalize!(definition)
  errors = DefinitionSchema.validate(definition)
  raise DefinitionInvalid, errors.map { |error| source ? "#{source}: #{error}" : error } if errors.any?

  definition
rescue Psych::Exception => error
  raise DefinitionInvalid, [source ? "#{source}: #{error.message}" : error.message]
end