Class: Moku6::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/moku6/loader.rb

Constant Summary collapse

PERMITTED_CLASSES =

: Array

[Symbol].freeze

Instance Method Summary collapse

Constructor Details

#initialize(catalog_dir) ⇒ Loader

: (String catalog_dir) -> void



11
# File 'lib/moku6/loader.rb', line 11

def initialize(catalog_dir) = @catalog_dir = catalog_dir

Instance Method Details

#loadObject

: () -> Catalog



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/moku6/loader.rb', line 14

def load
  unless Dir.exist?(@catalog_dir)
    raise UsageError, "catalog directory not found: #{@catalog_dir}"
  end

  paths = Dir.glob(File.join(@catalog_dir, "**", "*.{yml,yaml}")).sort
  events = paths.map do |path|
    data = begin
      YAML.safe_load_file(path, permitted_classes: PERMITTED_CLASSES)
    rescue Psych::SyntaxError => e
      raise UsageError, "failed to parse YAML (#{path}): #{e.message}"
    end
    Event.new(data, source_path: path)
  end
  Catalog.new(events)
end