Sha256: bbe2eae161198b55f33738418ecd0e897b2c25c4c34b0156cabdd5005cc334bd

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

module Eternity
  class Delta
    class << self

      def union(deltas)
        deltas.each_with_object({}) do |delta, hash|
          delta.each do |collection, elements|
            hash[collection] ||= {}
            elements.each do |id, change|
              hash[collection][id] ||= []
              hash[collection][id] << change
            end
          end
        end
      end

      def merge(deltas)
        union(deltas).each_with_object({}) do |(collection, elements), hash|
          hash[collection] = {}
          elements.each do |id, changes|
            change = TrackFlatter.flatten changes
            hash[collection][id] = TrackFlatter.flatten changes if change
          end
        end
      end

      def between(commit_1, commit_2)
        commits = ([commit_2] + commit_2.history) - ([commit_1] + commit_1.history)
        merge commits.reverse.map(&:delta)
      end

      def revert(delta, commit)
        commit.with_index do |index|
          delta.each_with_object({}) do |(collection, changes), hash|
            hash[collection] = {}
            changes.each do |id, change|
              hash[collection][id] = 
                case change['action']
                  when INSERT then {'action' => DELETE}
                  when UPDATE then {'action' => UPDATE, 'data' => index[collection][id].data}
                  when DELETE then {'action' => INSERT, 'data' => index[collection][id].data}
                end
            end
          end
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eternity-0.0.3 lib/eternity/delta.rb