
RailsEventStore::Event = RubyEventStore::Event

module RailsEventStore
  class Event < RubyEventStore::Event
    def initialize(**args)
      attributes = args.except(:event_type, :event_id, :metadata)
      singleton_class = (class << self; self; end)
      attributes.each do |key, value|
        singleton_class.send(:define_method, key) { value }
      end

      event_data = {
        event_type: args[:event_type] || self.class.name,
        data:       attributes,
      }
      event_data[:event_id] = args[:event_id] if args.key?(:event_id)
      event_data[:metadata] = args[:metadata] if args.key?(:metadata)
      super(event_data)
    end
  end
end

Improve RailsEventStore::Event - a little Ruby magic
Improve RailsEventStore::Event - a little Ruby magic

It is still the same RubyEventStore::Event as a base class.
However with new RailsEventStore::Event you could:

* Create new domain event easier:
  SomeEvent = Class.new(RailsEventStore::Event)
  ev = SomeEvent.new(name: 'Some', value: 123)

* Have easier access to event's attributes:
  name = ev.name

* Easier event publish:
  client.publish_event(SomeEvent.new(name: 'Some', value: 123, metadata: { correlation_id: SecureRandom.uuid })
