Sha256: 438910035f76b5f4c1451fc89aa13ce80a319f52691ea76f2f62190f755cf30f

Contents?: true

Size: 843 Bytes

Versions: 2

Compression:

Stored size: 843 Bytes

Contents

module EventSourcery
  class Repository
    def self.load(aggregate_class, aggregate_id, event_source:, event_sink:)
      new(event_source: event_source, event_sink: event_sink)
        .load(aggregate_class, aggregate_id)
    end

    def initialize(event_source:, event_sink:)
      @event_source = event_source
      @event_sink = event_sink
    end

    def load(aggregate_class, aggregate_id)
      events = event_source.get_events_for_aggregate_id(aggregate_id)
      aggregate_class.new(aggregate_id, events)
    end

    def save(aggregate)
      new_events = aggregate.changes
      if new_events.any?
        event_sink.sink(new_events,
                        expected_version: aggregate.version - new_events.count)
      end
      aggregate.clear_changes
    end

    private

    attr_reader :event_source, :event_sink
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
event_sourcery-0.14.0 lib/event_sourcery/repository.rb
event_sourcery-0.13.0 lib/event_sourcery/repository.rb