Class: HomeWorkChecker::TestRunStat::RubyStat

Inherits:
Object
  • Object
show all
Defined in:
lib/hw_checker/ruby_stat.rb

Instance Method Summary collapse

Constructor Details

#initialize(tmp_path, dirname) ⇒ RubyStat

Returns a new instance of RubyStat.



4
5
6
7
8
# File 'lib/hw_checker/ruby_stat.rb', line 4

def initialize(tmp_path, dirname)
  @work_path = "#{tmp_path}/#{dirname}" 
  raise DirectoryExistError, "Archive '#{dirname}' is invalid" unless Dir::exist?(@work_path)
  @lines_all = @lines_failed = 0
end

Instance Method Details

#calc_percent_qualityObject



33
34
35
36
# File 'lib/hw_checker/ruby_stat.rb', line 33

def calc_percent_quality
  return 0.00 if @lines_all.zero?
  ( (1.0 - @lines_failed.to_f / @lines_all) * 100).round(2)
end

#count_lines_all(filename) ⇒ Object



29
30
31
# File 'lib/hw_checker/ruby_stat.rb', line 29

def count_lines_all(filename)
  File.open(filename).each_line { @lines_all += 1 }        
end

#count_lines_failed(filename) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/hw_checker/ruby_stat.rb', line 20

def count_lines_failed(filename)
  temp = []
  File.open(filename).each_line do |line|
    next unless line.match(/[CW]:\s+\d+:\s+[\w\W]{4,}/)
    temp << line.scan(/\d+/).first.to_i
  end
  @lines_failed += temp.uniq.size
end

#performObject



10
11
12
13
14
15
16
17
18
# File 'lib/hw_checker/ruby_stat.rb', line 10

def perform
  Dir.foreach(@work_path) do |p|
    next unless File::file?("#{@work_path}/#{p}") && File.extname(p) == '.rb'
    `rubocop #{@work_path}/#{p} > #{@work_path}/#{p}.tmp`
    count_lines_failed("#{@work_path}/#{p}.tmp")
    count_lines_all("#{@work_path}/#{p}")
  end
  calc_percent_quality
end