Sha256: 07ca9f01630415bc3af6493ff1be62ee46af07ea219d9df57b335ce09f11484d

Contents?: true

Size: 894 Bytes

Versions: 2

Compression:

Stored size: 894 Bytes

Contents

# frozen_string_literal: true

module Umwelt::History
  class Aggregate
    include Hanami::Interactor

    expose :fragments

    def initialize
      @engaged = {}
      @forgotten = []
    end

    def call(episodes)
      episodes.each do |episode|
        index_engaged(episode.engaged)
        collect_forgotten(episode.forgotten)
      end

      verify_consistency

      @fragments = @engaged.slice(*alive_ids).values
    end

    private

    def alive_ids
      @engaged.keys - @forgotten
    end

    def fantom_ids
      @forgotten - @engaged.keys
    end

    def verify_consistency
      fantom_ids.empty? || error!("Aggregate failed: fantom ids: #{fantom_ids}")
    end

    def collect_forgotten(ids)
      @forgotten.push(*ids)
    end

    def index_engaged(fragments)
      fragments.each do |fragment|
        @engaged[fragment[:id]] = fragment
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
umwelt-0.1.1 lib/umwelt/history/aggregate.rb
umwelt-0.1.0 lib/umwelt/history/aggregate.rb