Module: Trak
- Defined in:
- lib/trak.rb
Constant Summary collapse
- TIME_FORMAT_12HOUR =
"%l:%M %p"
- TIME_FORMAT_24HOUR =
"%k:%M"
Class Method Summary collapse
- .breakpoint(steps = 1) ⇒ Object
-
.minutesToTime(minutes) ⇒ Object
expects an integer.
- .newTimeWithMinutes(start_time, minutes) ⇒ Object
-
.printSubReport(report_hash, report_title) ⇒ Object
expects a hash of tasks mapped to time spent, and a sub-report name (e.g., work, personal) prints a formatted sub-report returns the total hours worked.
-
.processTimeArgument(time_string) ⇒ Object
expects a single argument - the time argument in the format ##m or ##h if argument has no m/h qualifier, assume m returns a number of minutes.
-
.startTimeInMinutes(minutes) ⇒ Object
expects an integer which is the amount of minutes logged.
- .time_with_hours_minutes(*hm) ⇒ Object
-
.timeString(minutes) ⇒ Object
expects a number of minutes if less than 60 returns the number with an “m” otherwise converts to hours and adds an “h”.
- .to12HourTime(time) ⇒ Object
Class Method Details
.breakpoint(steps = 1) ⇒ Object
9 10 11 |
# File 'lib/trak.rb', line 9 def self.breakpoint(steps = 1) debugger(steps) if ENV['TRAK_DEBUG'] == "1" end |
.minutesToTime(minutes) ⇒ Object
expects an integer
89 90 91 |
# File 'lib/trak.rb', line 89 def self.minutesToTime(minutes) time_with_hours_minutes(minutes / 60, minutes % 60).strftime(TIME_FORMAT_24HOUR).strip end |
.newTimeWithMinutes(start_time, minutes) ⇒ Object
54 55 56 57 |
# File 'lib/trak.rb', line 54 def self.newTimeWithMinutes(start_time, minutes) hm = start_time.split ':' Time.at(time_with_hours_minutes(*hm).to_i + minutes.to_i*60).strftime(TIME_FORMAT_24HOUR).strip end |
.printSubReport(report_hash, report_title) ⇒ Object
expects a hash of tasks mapped to time spent, and a sub-report name
(e.g., work, personal)
prints a formatted sub-report returns the total hours worked
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/trak.rb', line 17 def self.printSubReport(report_hash, report_title) total = 0 unless report_hash.empty? count = 0 report_out = "" report_hash.each do |title, minutes| total += minutes.to_i count += 1 report_out += "=> #{timeString(minutes)}: #{title}" report_out += "\n" unless count == report_hash.size end puts "# #{report_title} time (#{timeString(total)})" puts report_out end total end |
.processTimeArgument(time_string) ⇒ Object
expects a single argument - the time argument in the format ##m or ##h if argument has no m/h qualifier, assume m returns a number of minutes
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/trak.rb', line 69 def self.processTimeArgument(time_string) if time_string =~ /^(\d*\.?\d+)((m|min|minute|minutes)|(h|hr|hour|hours))?$/i time = $1.to_f modifier = $2 minutes = (modifier =~ /h.*/) ? time * 60 : time # check enough time has been logged if minutes < 15 STDERR.puts "You must log at least 15 minutes." exit 1 end minutes.round_to_nearest 15 else STDERR.puts "Incorrectly formatted argument." exit 1 end end |
.startTimeInMinutes(minutes) ⇒ Object
expects an integer which is the amount of minutes logged
94 95 96 |
# File 'lib/trak.rb', line 94 def self.startTimeInMinutes(minutes) Time.now.to_minutes.round_to_nearest(15) - minutes end |
.time_with_hours_minutes(*hm) ⇒ Object
34 35 36 37 |
# File 'lib/trak.rb', line 34 def self.time_with_hours_minutes(*hm) dmy = Time.now.to_a[3..5].reverse Time.new(*dmy, *hm) end |
.timeString(minutes) ⇒ Object
expects a number of minutes if less than 60 returns the number with an “m” otherwise converts to hours and adds an “h”
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/trak.rb', line 42 def self.timeString(minutes) if minutes >= 60 hours = minutes/60.0 if hours % 1 == 0 hours = hours.to_i end "#{hours}h" else "#{minutes}m" end end |
.to12HourTime(time) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/trak.rb', line 59 def self.to12HourTime(time) unless time.blank? hm = time.split ':' time_with_hours_minutes(*hm).strftime(TIME_FORMAT_12HOUR).strip end end |