Sha256: 93c0dc5ab5b1e36f778b183939d21bcb9f71788eebcabec965c993fe6d6f9c5e

Contents?: true

Size: 1.38 KB

Versions: 12

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module PgEventstore
  # @!visibility private
  class EventDeserializer
    # @!attribute middlewares
    #   @return [Array<#deserialize, #serialize>]
    attr_reader :middlewares
    # @!attribute event_class_resolver
    #   @return [#call]
    attr_reader :event_class_resolver

    # @param middlewares [Array<Object<#deserialize, #serialize>>]
    # @param event_class_resolver [#call]
    def initialize(middlewares, event_class_resolver)
      @middlewares = middlewares
      @event_class_resolver = event_class_resolver
    end

    # @param raw_events [Array<Hash>]
    # @return [Array<PgEventstore::Event>]
    def deserialize_many(raw_events)
      raw_events.map(&method(:deserialize))
    end

    # @param attrs [Hash]
    # @return [PgEventstore::Event]
    def deserialize(attrs)
      event = event_class_resolver.call(attrs['type']).new(**attrs.except('link').transform_keys(&:to_sym))
      middlewares.each do |middleware|
        middleware.deserialize(event)
      end
      event.stream = PgEventstore::Stream.new(
        **attrs.slice('context', 'stream_name', 'stream_id').transform_keys(&:to_sym)
      )
      event.link = without_middlewares.deserialize(attrs['link']) if attrs.key?('link')
      event
    end

    # @return [PgEventstore::EventDeserializer]
    def without_middlewares
      self.class.new([], event_class_resolver)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pg_eventstore-1.7.0 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.6.0 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.5.0 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.4.0 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.3.4 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.3.3 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.3.2 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.3.1 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.3.0 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.2.0 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.1.5 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.1.4 lib/pg_eventstore/event_deserializer.rb