Class: EventEngine::EventSchemaLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/event_engine/event_schema_loader.rb

Class Method Summary collapse

Class Method Details

.load(path) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/event_engine/event_schema_loader.rb', line 3

def self.load(path)
  registry = SchemaRegistry.new
  return registry unless File.exist?(path)

  contents = File.read(path.to_s)
  return registry if contents.strip.empty?

  sandbox = Module.new
  sandbox.const_set(:EventEngine, EventEngine)

  schema =
    sandbox.module_eval(contents, path.to_s)

  unless schema.is_a?(EventEngine::EventSchema)
    raise <<~MSG
      Invalid EventEngine schema file.

      Expected #{path} to return an EventSchema from:
        EventEngine::EventSchema.define { ... }

      But got:
        #{schema.inspect}
    MSG
  end

  schema.schemas_by_event.each_value do |versions|
    versions.each_value do |s|
      registry.register(s)
    end
  end

  registry
end