Module: Timetrap::Helpers

Included in:
CLI, Formatters::Text
Defined in:
lib/timetrap/helpers.rb

Instance Method Summary collapse

Instance Method Details

#format_date(time) ⇒ Object



22
23
24
25
# File 'lib/timetrap/helpers.rb', line 22

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



27
28
29
30
# File 'lib/timetrap/helpers.rb', line 27

def format_date_if_new time, last_time
  return '' unless time.respond_to?(:strftime)
  same_day?(time, last_time) ? '' : format_date(time)
end

#format_duration(stime, etime) ⇒ Object



36
37
38
39
40
# File 'lib/timetrap/helpers.rb', line 36

def format_duration stime, etime
  return '' unless stime and etime
  secs = etime.to_i - stime.to_i
  format_seconds secs
end

#format_seconds(secs) ⇒ Object



42
43
44
# File 'lib/timetrap/helpers.rb', line 42

def format_seconds secs
  "%2s:%02d:%02d" % [secs/3600, (secs%3600)/60, secs%60]
end

#format_time(time) ⇒ Object



17
18
19
20
# File 'lib/timetrap/helpers.rb', line 17

def format_time time
  return '' unless time.respond_to?(:strftime)
  time.strftime('%H:%M:%S')
end

#format_total(entries) ⇒ Object



46
47
48
49
# File 'lib/timetrap/helpers.rb', line 46

def format_total entries
  secs = entries.inject(0){|m, e|e_end = e.end_or_now; m += e_end.to_i - e.start.to_i if e_end && e.start;m}
  "%2s:%02d:%02d" % [secs/3600, (secs%3600)/60, secs%60]
end

#same_day?(time, other_time) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/timetrap/helpers.rb', line 32

def same_day? time, other_time
  format_date(time) == format_date(other_time)
end

#selected_entriesObject



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/timetrap/helpers.rb', line 4

def selected_entries
  ee = if (sheet = sheet_name_from_string(unused_args)) == 'all'
    Timetrap::Entry.filter('sheet not like ? escape "!"', '!_%')
  elsif sheet =~ /.+/
    Timetrap::Entry.filter('sheet = ?', sheet)
  else
    Timetrap::Entry.filter('sheet = ?', Timetrap.current_sheet)
  end
  ee = ee.filter(:start >= Date.parse(args['-s'])) if args['-s']
  ee = ee.filter(:start <= Date.parse(args['-e']) + 1) if args['-e']
  ee
end

#sheet_name_from_string(string) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/timetrap/helpers.rb', line 51

def sheet_name_from_string string
  return "all" if string =~ /^\W*all\W*$/
  return "" unless string =~ /.+/
  DB[:entries].filter(:sheet.like("#{string}%")).first[:sheet]
rescue
  ""
end