Class: Error

Inherits:
Object
  • Object
show all
Defined in:
lib/error-handling.rb

Instance Method Summary collapse

Instance Method Details

#countObject



38
39
40
41
# File 'lib/error-handling.rb', line 38

def count
	#returns the amount of errors currently stored.
	return $stored_errors.length
end

#create(*error_arguments) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/error-handling.rb', line 5

def create(*error_arguments)
	if error_arguments.length == 2
		#push new error to global errors array.

		#get current date
		time_date = Time.new
		time = time_date.strftime('%Y-%m-%d')
		$stored_errors.push([error_arguments[0], error_arguments[1], time])
	else
		#invalid number or parameters.
		puts("Invalid error syntax, at least 2 parameters expected.")
	end
end

#display(display_errors_ammount) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/error-handling.rb', line 19

def display(display_errors_ammount)
	if display_errors_ammount.kind_of? Integer
		if $stored_errors.length > display_errors_ammount
			#display error
			print($stored_errors[display_errors_ammount - 1][0] + ": " + $stored_errors[display_errors_ammount - 1][1])
		else
			puts("Invalid error syntax, invalid error number.")
		end
	else
		if display_errors_ammount == 'all'

			#loop through and disply the errors
			$stored_errors.each{ |indiviual_error|
				puts("[" + indiviual_error[2].to_s + "] " + indiviual_error[0] + ": " + indiviual_error[1])
			}
		end
	end
end

#erase(*include_check) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/error-handling.rb', line 61

def erase(*include_check)
	if include_check[0] == 'check'
		#provides more control
		print("You are about to erase your errors, would you like to continue? [Y/N]")
		
		#loop to ensure y or n is entered.
		while true
			answer = gets.chop
			if answer.upcase == 'Y'
				$stored_errors.clear
				break
			elsif answer.upcase == 'N'
				break
			end	
		end
	else
		#clear errors without check
		$stored_errors.clear
	end
	return
end

#export(errors_export_name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/error-handling.rb', line 43

def export(errors_export_name)
	
	#make sure 1 parameter is passed.
	if !errors_export_name
		puts("Invalid errors syntax, parameter expected \"filename\".")
	end

	#create error log file
	File.open(errors_export_name, "w") do |file|

		#loop through the errors putting them into the file
		for i in 0..$stored_errors.length - 1
			error_message = "[" + $stored_errors[i][2] + "] " + $stored_errors[i][0] + ": " + $stored_errors[i][1]
			file.puts(error_message)
		end
	end
end

#load(file_name) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/error-handling.rb', line 83

def load(file_name)
	#varify the file does inface exist
	if File.file?(file_name)

		#open error file and read each line.
		File.open(file_name, "r").each do |file_line|

			#split the items to push individually into the errors array
			error_split = file_line.split(" ")

			#push errors into the $stored_errors array
			$stored_errors.push([error_split[1].chop, error_split[2], error_split[0].slice(1..-1).chop])
		end
	else
		puts("Load error: \"" + file_name + "\" does not exist.")
	end
end

#search(error_type) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/error-handling.rb', line 101

def search(error_type)
	$stored_errors.each{ |error|
		if error[0] == error_type
			puts("[" + error[2] + "] " + error[0] + ": " + error[1])
		end
	}
end