Sha256: 9858144dea830c6c0eaef3b9de60f744fed7c51fa842173f5b99795816fd5355

Contents?: true

Size: 1.17 KB

Versions: 8

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module PgEventstore
  # @!visibility private
  class EventDeserializer
    attr_reader :middlewares, :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>]
    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.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

8 entries across 8 versions & 1 rubygems

Version Path
pg_eventstore-1.1.3 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.1.2 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.1.1 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.1.0 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.0.4 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.0.0.rc2 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-1.0.0.rc1 lib/pg_eventstore/event_deserializer.rb
pg_eventstore-0.10.2 lib/pg_eventstore/event_deserializer.rb