Sha256: 2b30d69dfd3059f23f70dee98025d4d92496491fa00b495e5990d814a8ee9eff
Contents?: true
Size: 1.65 KB
Versions: 2
Compression:
Stored size: 1.65 KB
Contents
# frozen_string_literal: true require "json" module ActiveEventStore using(Module.new do refine Hash do def symbolize_keys RubyEventStore::TransformKeys.symbolize(self) end end end) # Custom mapper for RES events. # # See https://github.com/RailsEventStore/rails_event_store/blob/v0.35.0/ruby_event_store/lib/ruby_event_store/mappers/default.rb class Mapper def initialize(mapping:, serializer: JSON) @serializer = serializer @mapping = mapping end def event_to_serialized_record(domain_event) # lazily add type to mapping # NOTE: use class name instead of a class to handle code reload # in development (to avoid accessing orphaned classes) mapping.register(domain_event.event_type, domain_event.class.name) unless mapping.exist?(domain_event.event_type) RubyEventStore::SerializedRecord.new( event_id: domain_event.event_id, metadata: serializer.dump(domain_event.metadata.to_h), data: serializer.dump(domain_event.data), event_type: domain_event.event_type ) end def serialized_record_to_event(record) event_class = mapping.fetch(record.event_type) do raise "Don't know how to deserialize event: \"#{record.event_type}\". " \ "Add explicit mapping: ActiveEventStore.mapper.register \"#{record.event_type}\", \"<Class Name>\"" end Object.const_get(event_class).new( **serializer.load(record.data).symbolize_keys, metadata: serializer.load(record.metadata).symbolize_keys, event_id: record.event_id ) end private attr_reader :serializer, :mapping end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
active_event_store-0.2.1 | lib/active_event_store/mapper.rb |
active_event_store-0.2.0 | lib/active_event_store/mapper.rb |