Sha256: 9c46b8e0e4dd868c147a06ee0f0c20633aa52138f73bc75d860deabf2c2d4c67

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require 'parallel'

require 'parser'
require 'parser/current' # to get the error message early and once before we parallel things

module Leftovers
  class Collector
    attr_writer :progress, :parallel
    attr_reader :collection

    def initialize
      @count = 0
      @count_calls = 0
      @count_definitions = 0
      @progress = true
      @parallel = true
      @collection ||= Collection.new
    end

    def collect
      collect_file_list(FileList.new)
      ::Leftovers.puts progress_message
    end

    def collect_file_list(list)
      if @parallel
        ::Parallel.each(list, finish: method(:finish_file)) do |file|
          collect_file(file)
        end
      else
        list.each { |file| finish_file(nil, nil, collect_file(file)) }
      end
    end

    def collect_file(file)
      file_collector = FileCollector.new(file.ruby, file)
      file_collector.collect

      file_collector.to_h
    end

    def progress_message
      "checked #{@count} files, collected #{@count_calls} calls, #{@count_definitions} definitions"
    end

    def finish_file(_item, _index, result)
      @count += 1
      @count_calls += result[:calls].length
      @count_definitions += result[:definitions].length
      ::Leftovers.print(progress_message) if @progress

      @collection.concat(**result)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
leftovers-0.12.2 lib/leftovers/collector.rb
leftovers-0.12.1 lib/leftovers/collector.rb
leftovers-0.12.0 lib/leftovers/collector.rb
leftovers-0.11.2 lib/leftovers/collector.rb