Class: Insurance::SourceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/insurance/source_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ SourceFile

Returns a new instance of SourceFile.



7
8
9
10
11
# File 'lib/insurance/source_file.rb', line 7

def initialize(name)
  @name             = name
  @lines            = []
  @coverage_percent = 0
end

Instance Attribute Details

#coverage_percentObject (readonly)

Returns the value of attribute coverage_percent.



5
6
7
# File 'lib/insurance/source_file.rb', line 5

def coverage_percent
  @coverage_percent
end

#linesObject (readonly)

Returns the value of attribute lines.



4
5
6
# File 'lib/insurance/source_file.rb', line 4

def lines
  @lines
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/insurance/source_file.rb', line 3

def name
  @name
end

Instance Method Details

#<<(line) ⇒ Object



13
14
15
# File 'lib/insurance/source_file.rb', line 13

def <<(line)
  @lines << line unless @lines.include?(line)
end

#post_analyze!Object

This marks crufty crap that isn’t important, like ‘end’, and whitespace.



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

def post_analyze!
  contents = File.open(self.name, 'r').readlines
  contents.each_with_index do |line, num|
    sline = line.strip
    lines << num + 1 if sline.empty?
    lines << num + 1 if sline =~ /^#/
    lines << num + 1 if sline =~ /^\s*(?:end|\})\s*(?:#.*)?$/
    lines << num + 1 if sline =~ /^(public|private|protected)/
    lines << num + 1 if sline =~ /^(?:begin\s*(?:#.*)?|ensure\s*(?:#.*)?|else\s*(?:#.*)?)$/
    lines << num + 1 if sline =~ /^(?:rescue)/
    lines << num + 1 if sline =~ /^case\s*(?:#.*)?$/
    lines << num + 1 if sline =~ /^(\)|\]|\})(?:#.*)?$/
  end
  
  @coverage_percent = (@lines.size.to_f / contents.size.to_f) * 100.0
end