Class: HMSTools::LOCReader

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

Overview

Basic Lines Of Code reader Searches through all subfolders You must set the extensions to search. Takes a string, comma seperated

Instance Method Summary collapse

Constructor Details

#initializeLOCReader

Returns a new instance of LOCReader.



51
52
53
54
55
56
# File 'lib/hmstools.rb', line 51

def initialize
	@lines = @lines.to_i
	@blank_lines = @blank_lines.to_i
	@num_files = @lines.to_i
	@total = @total.to_i
end

Instance Method Details

#get_files(dir) ⇒ Object

Grabs the directory and loops through the files. This is recursive to grab all sub directories

Example >> LOCReader.get_files “./my_project/*”

Arguments:

dir: (String)  The directory you want to search. Include /* on the end.


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/hmstools.rb', line 84

def get_files(dir)
	
	files = Dir.glob(dir)
	dir_path = dir.chomp("*")

	for file in files
		unless File.directory?(file)
			if @ext.include?(File.extname(file))
				read_lines file
				@num_files = @num_files+1
			end
			
		else
			get_files file+"/*"
		end

	end
end

Returns an array of the number of files, total lines, blank lines, and actual lines Example >> LOCReader.print_array

> [3, 90, 10, 100]



132
133
134
# File 'lib/hmstools.rb', line 132

def print_array
	[@num_files, @total, @blank_lines, @lines]
end

Example >> LOCReader.print_hash

> => 3, “loc” => 90, “bloc” => 10, “lines” => 100



141
142
143
# File 'lib/hmstools.rb', line 141

def print_hash
	{"files" => @num_files, "loc" => @total, "bloc" => @blank_lines, "lines" => @lines}
end

Example >> LOCReader.print_output

> Files 3

> Lines of Code: 90

> Blank Lines: 10

> Total Lines: 10



154
155
156
157
158
159
# File 'lib/hmstools.rb', line 154

def print_output
	print "Files: #@num_files \n"
	print "Lines of Code: #@total \n"
	print "Blank Lines: #@blank_lines \n"
	print "Total Lines: #@lines"
end

#read_lines(file) ⇒ Object

Reads the files and counts the lines

Example >> LOCReader.read_lines “./helloworld.rb”

> Hello World

Arguments:

file: (String)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/hmstools.rb', line 112

def read_lines(file)
	begin
		ofile = File.new(file, "r")
		while (line = ofile.gets)
			@lines += 1
			if line.strip == ""
				@blank_lines += 1
			end
		end
		ofile.close
	rescue => err
		puts "Could not access #{err}"
	end
end

#run(dir, ext = "") ⇒ Object

Starts the process of counting lines of code

Example: >> LOCReader.run “./my_project/*”, “.rb”

Arguments:

dir: (String)  The directory you want to search. Include /* on the end.
ext: (String)  List of file extensions. Comma seperated


67
68
69
70
71
72
73
74
# File 'lib/hmstools.rb', line 67

def run(dir, ext = "")
	unless ext == ""
		@ext = ext.split(',')
	end

	get_files dir
	@total = @lines - @blank_lines
end