Class: CoverRage::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/cover_rage/recorder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_prefix:, store:) ⇒ Recorder

Returns a new instance of Recorder.



11
12
13
14
15
16
# File 'lib/cover_rage/recorder.rb', line 11

def initialize(path_prefix:, store:)
  @store = store
  @path_prefix = path_prefix.end_with?('/') ? path_prefix : "#{path_prefix}/"
  @digest = Digest::MD5.new
  @file_cache = {}
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



9
10
11
# File 'lib/cover_rage/recorder.rb', line 9

def store
  @store
end

Instance Method Details

#save(coverage_result) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cover_rage/recorder.rb', line 35

def save(coverage_result)
  records = []
  coverage_result.map do |filepath, execution_count|
    filepath = File.expand_path(filepath) unless filepath.start_with?('/')
    next unless filepath.start_with?(@path_prefix)
    next if execution_count.all? { _1.nil? || _1.zero? }

    relative_path = filepath.delete_prefix(@path_prefix)
    revision, source = read_file_with_revision(filepath)

    now = Time.now.to_i
    last_executed_at = execution_count.map { |c| c&.positive? ? now : nil }

    records << Record.new(
      path: relative_path,
      revision:,
      source:,
      execution_count:,
      last_executed_at:
    )
  end
  return unless records.any?

  @store.transaction do
    records_to_save = Record.merge(@store.list, records)
    @store.update(records_to_save)
  end
end

#startObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cover_rage/recorder.rb', line 18

def start
  return if @thread&.alive?

  unless Coverage.running?
    Coverage.start
    at_exit { save(Coverage.result) }
  end
  @thread = Thread.new do
    interval = Config.interval
    jitter = 0.15
    loop do
      sleep(interval + (rand * interval * jitter))
      save(Coverage.result(stop: false, clear: true))
    end
  end
end