Class: Lyra::GenericAggregate

Inherits:
Aggregate show all
Defined in:
lib/lyra/aggregate.rb

Overview

Generic aggregate for monitored models

Instance Attribute Summary

Attributes inherited from Aggregate

#changes, #id, #version

Instance Method Summary collapse

Methods inherited from Aggregate

load, #store

Constructor Details

#initialize(id = nil, model_class = nil) ⇒ GenericAggregate

Returns a new instance of GenericAggregate.



70
71
72
73
# File 'lib/lyra/aggregate.rb', line 70

def initialize(id = nil, model_class = nil)
  super(id)
  @model_class = model_class
end

Instance Method Details

#apply(event, persisted: false) ⇒ Object

Override apply to handle dynamic event class names (e.g., RegistrationCreated -> apply_created)



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lyra/aggregate.rb', line 80

def apply(event, persisted: false)
  event_name = event.class.name.demodulize.underscore

  # Extract operation from event name (e.g., "registration_created" -> "created")
  operation = extract_operation(event_name)
  method_name = "apply_#{operation}"

  if respond_to?(method_name, true)
    send(method_name, event)
    @version += 1 if persisted
    @changes << event unless persisted
  end
end

#stream_name ⇒ Object



75
76
77
# File 'lib/lyra/aggregate.rb', line 75

def stream_name
  "#{@model_class.name}$#{id}"
end