Class: Lyra::EventMapper
- Inherits:
-
Object
- Object
- Lyra::EventMapper
- Defined in:
- lib/lyra/event_mapper.rb
Overview
Maps CRUD operations to domain events
Direct Known Subclasses
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#operation ⇒ Object
readonly
Returns the value of attribute operation.
Class Method Summary collapse
-
.map_operation(model_class, operation, data) ⇒ Object
Map a CRUD operation to an event.
-
.register_mapper(model_class, mapper_class) ⇒ Object
Register a custom mapper for a model.
Instance Method Summary collapse
- #create_dynamic_event_class(event_name) ⇒ Object
- #event_data ⇒ Object
- #event_metadata ⇒ Object
-
#initialize(model_class, operation, data) ⇒ EventMapper
constructor
A new instance of EventMapper.
- #resolve_event_class ⇒ Object
- #to_event ⇒ Object
Constructor Details
#initialize(model_class, operation, data) ⇒ EventMapper
Returns a new instance of EventMapper.
30 31 32 33 34 |
# File 'lib/lyra/event_mapper.rb', line 30 def initialize(model_class, operation, data) @model_class = model_class @operation = operation @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
28 29 30 |
# File 'lib/lyra/event_mapper.rb', line 28 def data @data end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
28 29 30 |
# File 'lib/lyra/event_mapper.rb', line 28 def model_class @model_class end |
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
28 29 30 |
# File 'lib/lyra/event_mapper.rb', line 28 def operation @operation end |
Class Method Details
.map_operation(model_class, operation, data) ⇒ Object
Map a CRUD operation to an event
6 7 8 9 10 |
# File 'lib/lyra/event_mapper.rb', line 6 def map_operation(model_class, operation, data) mapper_class = find_mapper(model_class) || DefaultMapper mapper_class.new(model_class, operation, data).to_event end |
.register_mapper(model_class, mapper_class) ⇒ Object
Register a custom mapper for a model
13 14 15 |
# File 'lib/lyra/event_mapper.rb', line 13 def register_mapper(model_class, mapper_class) mappers[model_class.name] = mapper_class end |
Instance Method Details
#create_dynamic_event_class(event_name) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/lyra/event_mapper.rb', line 81 def create_dynamic_event_class(event_name) # Sanitize namespaced event names (e.g., "Spree::OrderCreated" -> "SpreeOrderCreated") sanitized_name = event_name.to_s.gsub("::", "") return Lyra::Events.const_get(sanitized_name, false) if Lyra::Events.const_defined?(sanitized_name, false) Lyra::Events.const_set(sanitized_name, Class.new(Lyra::Event)) end |
#event_data ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lyra/event_mapper.rb', line 41 def event_data { model_class: model_class.name, model_id: data[:id], operation: operation, attributes: data[:attributes] || {}, changes: data[:changes] || {}, timestamp: Time.current } end |
#event_metadata ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/lyra/event_mapper.rb', line 52 def { user_id: data[:user_id], request_id: data[:request_id], correlation_id: Lyra::Correlation.current_id, causation_id: Lyra::Causation.current_id, source: 'lyra_interceptor' } end |
#resolve_event_class ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/lyra/event_mapper.rb', line 62 def resolve_event_class config = Lyra.config.model_config(model_class) event_name = config.event_name_for(operation) # Sanitize namespaced event names for constant lookup sanitized_name = event_name.to_s.gsub("::", "") # First check if it exists in Lyra::Events namespace if Lyra::Events.const_defined?(sanitized_name, false) return Lyra::Events.const_get(sanitized_name, false) end # Try to constantize (for user-defined event classes) begin event_name.constantize rescue NameError create_dynamic_event_class(event_name) end end |
#to_event ⇒ Object
36 37 38 39 |
# File 'lib/lyra/event_mapper.rb', line 36 def to_event event_class = resolve_event_class event_class.new(data: event_data, metadata: ) end |