Class: Gitlab::DiscussionsDiff::FileCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Utils::StrongMemoize
Defined in:
lib/gitlab/discussions_diff/file_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ FileCollection

Returns a new instance of FileCollection.



9
10
11
# File 'lib/gitlab/discussions_diff/file_collection.rb', line 9

def initialize(collection)
  @collection = collection
end

Instance Method Details

#each(&block) ⇒ Object



13
14
15
# File 'lib/gitlab/discussions_diff/file_collection.rb', line 13

def each(&block)
  @collection.each(&block)
end

#find_by_id(id) ⇒ Object

Returns a Gitlab::Diff::File with the given ID (‘unique_identifier` in Gitlab::Diff::File).



19
20
21
# File 'lib/gitlab/discussions_diff/file_collection.rb', line 19

def find_by_id(id)
  diff_files_indexed_by_id[id]
end

#load_highlightObject

Writes cache and preloads highlighted diff lines for highlightable object IDs, in @collection.

  • Highlight cache is written just for uncached diff files

  • The cache content is not updated (there’s no need to do so)



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gitlab/discussions_diff/file_collection.rb', line 28

def load_highlight
  ids = highlightable_collection_ids
  return if ids.empty?

  cached_content = read_cache(ids)

  uncached_ids = ids.select.each_with_index { |_, i| cached_content[i].nil? }
  mapping = highlighted_lines_by_ids(uncached_ids)

  HighlightCache.write_multiple(mapping) if mapping.any?

  diffs = diff_files_indexed_by_id.values_at(*ids)

  diffs.zip(cached_content).each do |diff, cached_lines|
    next unless diff && cached_lines

    diff.highlighted_diff_lines = cached_lines
  end
end