Sha256: 9fabfa35f6b971392264b657dfc891c90156222a37cab8de5f5c432dc53d1fe9

Contents?: true

Size: 1.59 KB

Versions: 18

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module PgEventstore
  module Commands
    module EventModifiers
      # Defines how to transform regular event into a link event
      # @!visibility private
      class PrepareLinkEvent
        attr_reader :partition_queries, :partitions

        # @param partition_queries [PgEventstore::PartitionQueries]
        def initialize(partition_queries)
          @partitions = {}
          @partition_queries = partition_queries
        end
        # @param event [PgEventstore::Event]
        # @param revision [Integer]
        # @return [PgEventstore::Event]
        def call(event, revision)
          Event.new(
            link_id: event.id, link_partition_id: partition_id(event), type: Event::LINK_TYPE, stream_revision: revision
          ).tap do |e|
            %i[link_id link_partition_id type stream_revision].each { |attr| e.readonly!(attr) }
          end
        end

        private

        # @param event [PgEventstore::Event] persisted event
        # @return [Integer] partition id
        # @raise [PgEventstore::MissingPartitionError]
        def partition_id(event)
          partition_id = @partitions.dig(event.stream.context, event.stream.stream_name, event.type)
          return partition_id if partition_id

          partition_id = partition_queries.event_type_partition(event.stream, event.type)['id']
          @partitions[event.stream.context] ||= {}
          @partitions[event.stream.context][event.stream.stream_name] ||= {}
          @partitions[event.stream.context][event.stream.stream_name][event.type] = partition_id
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
pg_eventstore-1.6.0 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.5.0 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.4.0 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.3.4 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.3.3 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.3.2 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.3.1 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.3.0 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.2.0 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.1.5 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.1.4 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.1.3 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.1.2 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.1.1 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.1.0 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.0.4 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.0.0.rc2 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb
pg_eventstore-1.0.0.rc1 lib/pg_eventstore/commands/event_modifiers/prepare_link_event.rb