Class: RSpec::Covers::CodeRegion

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/covers/code_location.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file:, lines:, label:) ⇒ CodeRegion

Returns a new instance of CodeRegion.



20
21
22
23
24
# File 'lib/rspec/covers/code_location.rb', line 20

def initialize(file:, lines:, label:)
  @file = File.expand_path(file)
  @lines = Set.new(lines.map(&:to_i))
  @label = label
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



18
19
20
# File 'lib/rspec/covers/code_location.rb', line 18

def file
  @file
end

#labelObject (readonly)

Returns the value of attribute label.



18
19
20
# File 'lib/rspec/covers/code_location.rb', line 18

def label
  @label
end

#linesObject (readonly)

Returns the value of attribute lines.



18
19
20
# File 'lib/rspec/covers/code_location.rb', line 18

def lines
  @lines
end

Class Method Details

.file(file, label: file) ⇒ Object



42
43
44
45
46
# File 'lib/rspec/covers/code_location.rb', line 42

def self.file(file, label: file)
  line_count = File.exist?(file) ? File.readlines(file).length : 0

  new(file: file, lines: 1..line_count, label: label)
end

Instance Method Details

#include?(location) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rspec/covers/code_location.rb', line 26

def include?(location)
  file == File.expand_path(location.file) && lines.include?(location.line)
end

#locationsObject



30
31
32
# File 'lib/rspec/covers/code_location.rb', line 30

def locations
  lines.map { |line| CodeLocation.new(file: file, line: line) }.to_set
end

#to_hObject



34
35
36
37
38
39
40
# File 'lib/rspec/covers/code_location.rb', line 34

def to_h
  {
    file: file,
    lines: lines.to_a.sort,
    label: label
  }
end