Module: Timetrap::Helpers
- Included in:
- CLI, Formatters::Text
- Defined in:
- lib/timetrap/helpers.rb
Defined Under Namespace
Modules: AutoLoad
Instance Method Summary collapse
- #format_date(time) ⇒ Object
- #format_date_if_new(time, last_time) ⇒ Object
- #format_seconds(secs) ⇒ Object (also: #format_duration)
- #format_time(time) ⇒ Object
- #format_total(entries) ⇒ Object
- #same_day?(time, other_time) ⇒ Boolean
- #selected_entries ⇒ Object
- #sheet_name_from_string(string) ⇒ Object
Instance Method Details
#format_date(time) ⇒ Object
61 62 63 64 |
# File 'lib/timetrap/helpers.rb', line 61 def format_date time return '' unless time.respond_to?(:strftime) time.strftime('%a %b %d, %Y') end |
#format_date_if_new(time, last_time) ⇒ Object
66 67 68 69 |
# File 'lib/timetrap/helpers.rb', line 66 def format_date_if_new time, last_time return '' unless time.respond_to?(:strftime) same_day?(time, last_time) ? '' : format_date(time) end |
#format_seconds(secs) ⇒ Object Also known as: format_duration
75 76 77 78 79 80 81 |
# File 'lib/timetrap/helpers.rb', line 75 def format_seconds secs negative = secs < 0 secs = secs.abs formatted = "%2s:%02d:%02d" % [secs/3600, (secs%3600)/60, secs%60] formatted = "-#{formatted}" if negative formatted end |
#format_time(time) ⇒ Object
56 57 58 59 |
# File 'lib/timetrap/helpers.rb', line 56 def format_time time return '' unless time.respond_to?(:strftime) time.strftime('%H:%M:%S') end |
#format_total(entries) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/timetrap/helpers.rb', line 84 def format_total entries secs = entries.inject(0) do |m, e| m += e.duration end "%2s:%02d:%02d" % [secs/3600, (secs%3600)/60, secs%60] end |
#same_day?(time, other_time) ⇒ Boolean
71 72 73 |
# File 'lib/timetrap/helpers.rb', line 71 def same_day? time, other_time format_date(time) == format_date(other_time) end |
#selected_entries ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/timetrap/helpers.rb', line 41 def selected_entries ee = if (sheet = sheet_name_from_string(unused_args)) == 'all' Timetrap::Entry.filter('sheet not like ? escape "!"', '!_%') elsif (sheet = sheet_name_from_string(unused_args)) == 'full' Timetrap::Entry.filter() elsif sheet =~ /.+/ Timetrap::Entry.filter('sheet = ?', sheet) else Timetrap::Entry.filter('sheet = ?', Timer.current_sheet) end ee = ee.filter('start >= ?', Date.parse(Timer.process_time(args['-s']).to_s)) if args['-s'] ee = ee.filter('start <= ?', Date.parse(Timer.process_time(args['-e']).to_s) + 1) if args['-e'] ee end |
#sheet_name_from_string(string) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/timetrap/helpers.rb', line 91 def sheet_name_from_string string string = string.strip case string when /^\W*all\W*$/ then "all" when /^\W*full\W*$/ then "full" when /^$/ then Timer.current_sheet else entry = DB[:entries].filter(Sequel.like(:sheet,string)).first || DB[:entries].filter(Sequel.like(:sheet, "#{string}%")).first if entry entry[:sheet] else raise "Can't find sheet matching #{string.inspect}" end end end |