Sha256: 39411ea55e6fde519d1dedac303ff1bee34c9187b9255ff40d66338b509c18fd

Contents?: true

Size: 1.85 KB

Versions: 5

Compression:

Stored size: 1.85 KB

Contents

module LedgerSync
  module Util
    class Coordinator
      attr_reader :operation

      def initialize(operation:)

        @operation = operation
      end

      def operations
        @operations ||= begin
          flattened_operation = self.class.flatten_operation(
            operation
          )

          converted_operations = self.class.convert_downstream_creates_to_upserts(
            flattened_operation
          )

          de_duped = self.class.de_dup(converted_operations)

          de_duped
        end
      end

      # If we have already seen a create for the same object
      def self.convert_downstream_creates_to_upserts(ops)
        cache = {}
        ops.flat_map do |op|
          cache_key = "#{op.class.name}/#{op.resource.external_id}"
          if op.create? && cache.key?(cache_key)
            flatten_operation(op.convert_to_update)
          else
            cache[cache_key] = nil
            op
          end
        end
      end

      def self.de_dup(ops)
        @de_dup_cache = {}

        ops.select do |op|
          cache_key = "#{op.class.name}/#{op.resource.external_id}/#{op.fingerprint}"

          if @de_dup_cache.key?(cache_key)
            false
          else
            @de_dup_cache[cache_key] = true
          end
        end
      end

      # To build the ordered operations, start at root operation,
      # perform all before_operations in order, perform root,
      # then perform all after_operations in order,
      # recursively doing the same for all operations.
      def self.flatten_operation(op)
        op.prepare

        raise "root_operation required: #{op.inspect}" if op.root_operation.nil?

        [
          op.before_operations.map { |e| flatten_operation(e) },
          op.root_operation,
          op.after_operations.map { |e| flatten_operation(e) }
        ].flatten
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ledger_sync-1.0.10 lib/ledger_sync/util/coordinator.rb
ledger_sync-1.0.9 lib/ledger_sync/util/coordinator.rb
ledger_sync-1.0.3 lib/ledger_sync/util/coordinator.rb
ledger_sync-1.0.2 lib/ledger_sync/util/coordinator.rb
ledger_sync-1.0.0 lib/ledger_sync/util/coordinator.rb