Method: Countless::Statistics#initialize

Defined in:
lib/countless/statistics.rb

#initialize(*dirs) ⇒ Countless::Statistics

Initialize a new source code statistics displaying handler. When no configurations are passed in directly, we fallback to the configured statistics directories of the gem.

Parameters:

  • dirs (Array<Hash{Symbol => Mixed}>)

    the configurations



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/countless/statistics.rb', line 17

def initialize(*dirs)
  base_path = Countless.configuration.base_path

  # Resolve the given directory configurations to actual files
  dirs = dirs.presence || Countless.statistic_directories
  @dirs = dirs.each_with_object([]) do |cur, memo|
    copy = cur.deep_dup
    copy[:files] = Array(copy[:files])

    if copy[:pattern].is_a? Regexp
      copy[:files] += Dir[
        File.join(copy[:dir] || base_path, '**/*')
      ].select { |path| File.file?(path) && copy[:pattern].match?(path) }
    else
      copy[:files] += Dir[copy[:pattern]]
    end

    copy[:files].uniq!
    memo << copy if copy[:files].present?
  end

  @statistics = calculate_statistics
  @total = calculate_total if @dirs.length > 1
end