Class: EventEngine::DslCompiler
- Inherits:
-
Object
- Object
- EventEngine::DslCompiler
show all
- Defined in:
- lib/event_engine/dsl_compiler.rb
Defined Under Namespace
Classes: InvalidEventNameError, ReservedInputNameError
Constant Summary
collapse
- SNAKE_CASE =
/\A[a-z][a-z0-9_]*\z/
- RESERVED_INPUT_NAMES =
%i[
event_version
occurred_at
metadata
idempotency_key
aggregate_type
aggregate_id
aggregate_version
].freeze
Class Method Summary
collapse
Class Method Details
.compile(definitions, subject_registry: SubjectRegistry.new) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/event_engine/dsl_compiler.rb', line 18
def self.compile(definitions, subject_registry: SubjectRegistry.new)
registry = SchemaRegistry.new
subject_violations = []
name_violations = []
reserved_violations = []
Array(definitions).each do |definition|
schema = definition.schema
record_subject_violation(schema, subject_violations, subject_registry)
record_name_violation(schema, name_violations)
record_reserved_input_violation(schema, reserved_violations)
registry.register(schema)
end
raise_invalid_event_names(name_violations)
raise_reserved_input_names(reserved_violations)
raise_unknown_subjects(subject_violations)
registry
end
|
.raise_invalid_event_names(violations) ⇒ Object
45
46
47
48
49
|
# File 'lib/event_engine/dsl_compiler.rb', line 45
def self.raise_invalid_event_names(violations)
return if violations.empty?
raise InvalidEventNameError, "event names must be snake_case: #{violations.join(", ")}"
end
|
59
60
61
62
63
64
|
# File 'lib/event_engine/dsl_compiler.rb', line 59
def self.raise_reserved_input_names(violations)
return if violations.empty?
raise ReservedInputNameError,
"input names collide with reserved envelope keys: #{violations.join("; ")}"
end
|
.raise_unknown_subjects(violations) ⇒ Object
73
74
75
76
77
|
# File 'lib/event_engine/dsl_compiler.rb', line 73
def self.raise_unknown_subjects(violations)
return if violations.empty?
raise SubjectRegistry::UnknownSubjectError, violations.join(", ")
end
|
.record_name_violation(schema, violations) ⇒ Object
39
40
41
42
43
|
# File 'lib/event_engine/dsl_compiler.rb', line 39
def self.record_name_violation(schema, violations)
return if schema.event_name.to_s.match?(SNAKE_CASE)
violations << schema.event_name.inspect
end
|
51
52
53
54
55
56
57
|
# File 'lib/event_engine/dsl_compiler.rb', line 51
def self.record_reserved_input_violation(schema, violations)
inputs = schema.required_inputs + schema.optional_inputs
collisions = inputs & RESERVED_INPUT_NAMES
return if collisions.empty?
violations << "#{schema.event_name}: #{collisions.join(", ")}"
end
|
.record_subject_violation(schema, violations, subject_registry) ⇒ Object
66
67
68
69
70
71
|
# File 'lib/event_engine/dsl_compiler.rb', line 66
def self.record_subject_violation(schema, violations, subject_registry)
return if schema.subject.nil?
return if subject_registry.registered?(schema.subject)
violations << "#{schema.event_name}: unknown subject #{schema.subject.inspect}"
end
|