summaryrefslogtreecommitdiff
path: root/app/services/projects/batch_count_service.rb
blob: 93c5022a14d5cecf41d3f1d135f493fd824db14a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true

# Service class for getting and caching the number of elements of several projects
# Warning: do not user this service with a really large set of projects
# because the service use maps to retrieve the project ids.
module Projects
  class BatchCountService
    def initialize(projects)
      @projects = projects
    end

    def refresh_cache
      @projects.each do |project|
        service = count_service.new(project)
        unless service.count_stored?
          service.refresh_cache { global_count[project.id].to_i }
        end
      end
    end

    def project_ids
      @projects.map(&:id)
    end

    def global_count(project)
      raise NotImplementedError, _('global_count must be implemented and return an hash indexed by the project id')
    end

    def count_service
      raise NotImplementedError, _('count_service must be implemented and return a Projects::CountService object')
    end
  end
end