2019-10-09 20:06:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class PositionCollection
|
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
# collection - An array of Gitlab::Diff::Position
|
2019-10-16 14:08:01 -04:00
|
|
|
def initialize(collection, diff_head_sha = nil)
|
2019-10-09 20:06:44 -04:00
|
|
|
@collection = collection
|
|
|
|
@diff_head_sha = diff_head_sha
|
|
|
|
end
|
|
|
|
|
|
|
|
def each(&block)
|
2019-10-16 14:08:01 -04:00
|
|
|
filtered_positions.each(&block)
|
2019-10-09 20:06:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def concat(positions)
|
|
|
|
tap { @collection.concat(positions) }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Doing a lightweight filter in-memory given we're not prepared for querying
|
|
|
|
# positions (https://gitlab.com/gitlab-org/gitlab/issues/33271).
|
|
|
|
def unfoldable
|
|
|
|
select do |position|
|
2019-10-16 14:08:01 -04:00
|
|
|
position.unfoldable? && valid_head_sha?(position)
|
2019-10-09 20:06:44 -04:00
|
|
|
end
|
|
|
|
end
|
2019-10-16 14:08:01 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def filtered_positions
|
|
|
|
@collection.select { |item| item.is_a?(Position) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_head_sha?(position)
|
|
|
|
return true unless @diff_head_sha
|
|
|
|
|
|
|
|
position.head_sha == @diff_head_sha
|
|
|
|
end
|
2019-10-09 20:06:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|