lib/eternity/delta.rb in eternity-0.0.3 vs lib/eternity/delta.rb in eternity-0.0.4
- old
+ new
@@ -12,36 +12,44 @@
end
end
end
end
- def merge(deltas)
+ def merge(deltas, base_index)
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
+ base_data = base_index[collection].include?(id) ? base_index[collection][id].data : {}
+ changes.each do |change|
+ current_change = change
+ if hash[collection][id]
+ current_change = TrackFlatter.flatten [hash[collection][id], change]
+ if current_change && [INSERT, UPDATE].include?(current_change['action'])
+ current_change['data'] = ConflictResolver.resolve hash[collection][id]['data'] || base_data,
+ change['data'],
+ base_data
+ end
+ elsif hash[collection].key?(id) && change['action'] == DELETE
+ current_change = nil
+ end
+ hash[collection][id] = current_change
+ end
+ hash[collection].delete id unless hash[collection][id]
end
- end
+ hash.delete collection if hash[collection].empty?
+ 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
+ def revert(delta, 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
\ No newline at end of file