Class: Leftovers::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/leftovers/collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCollector

Returns a new instance of Collector.



13
14
15
16
17
18
19
20
# File 'lib/leftovers/collector.rb', line 13

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

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



11
12
13
# File 'lib/leftovers/collector.rb', line 11

def collection
  @collection
end

#parallel=(value) ⇒ Object (writeonly)

Sets the attribute parallel

Parameters:

  • value

    the value to set the attribute parallel to.



10
11
12
# File 'lib/leftovers/collector.rb', line 10

def parallel=(value)
  @parallel = value
end

#progress=(value) ⇒ Object (writeonly)

Sets the attribute progress

Parameters:

  • value

    the value to set the attribute progress to.



10
11
12
# File 'lib/leftovers/collector.rb', line 10

def progress=(value)
  @progress = value
end

Instance Method Details

#collectObject



22
23
24
25
# File 'lib/leftovers/collector.rb', line 22

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

#collect_file(file) ⇒ Object



37
38
39
40
41
42
# File 'lib/leftovers/collector.rb', line 37

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

  file_collector.to_h
end

#collect_file_list(list) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/leftovers/collector.rb', line 27

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

#finish_file(_item, _index, result) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/leftovers/collector.rb', line 48

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

#progress_messageObject



44
45
46
# File 'lib/leftovers/collector.rb', line 44

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