Module: Timetrap::Timer

Extended by:
Timer
Included in:
Timer
Defined in:
lib/timetrap/timer.rb

Defined Under Namespace

Classes: AlreadyRunning

Instance Method Summary collapse

Instance Method Details

#active_entry(sheet = nil) ⇒ Object



82
83
84
# File 'lib/timetrap/timer.rb', line 82

def active_entry(sheet=nil)
  Entry.find(:sheet => (sheet || Timer.current_sheet), :end => nil)
end

#current_sheetObject



62
63
64
65
66
67
# File 'lib/timetrap/timer.rb', line 62

def current_sheet
  unless Meta.find(:key => 'current_sheet')
    Meta.create(:key => 'current_sheet', :value => 'default')
  end
  Meta.find(:key => 'current_sheet').value
end

#current_sheet=(sheet) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/timetrap/timer.rb', line 52

def current_sheet= sheet
  last = Meta.find_or_create(:key => 'last_sheet')
  last.value = current_sheet
  last.save

  m = Meta.find_or_create(:key => 'current_sheet')
  m.value = sheet
  m.save
end

#entries(sheet = nil) ⇒ Object



74
75
76
# File 'lib/timetrap/timer.rb', line 74

def entries sheet = nil
  Entry.filter(:sheet => sheet).order_by(:start)
end

#last_checkoutObject

the last entry to be checked out of



87
88
89
90
# File 'lib/timetrap/timer.rb', line 87

def last_checkout
  meta = Meta.find(:key => 'last_checkout_id')
  Entry[meta.value] if meta
end

#last_sheetObject



69
70
71
72
# File 'lib/timetrap/timer.rb', line 69

def last_sheet
  m = Meta.find(:key => 'last_sheet')
  m and m.value
end

#process_time(time, now = Time.now) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/timetrap/timer.rb', line 11

def process_time(time, now = Time.now)
  case time
  when Time
    time
  when String
    chronic = begin
      time_closest_to_now_with_chronic(time, now)
    rescue => e
      warn "#{e.class} in Chronic gem parsing time.  Falling back to Time.parse"
    end

    if parsed = chronic
      parsed
    elsif safe_for_time_parse?(time) and parsed = Time.parse(time)
      parsed
    else
      raise ArgumentError, "Could not parse #{time.inspect}, entry not updated"
    end
  end
end

#running?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/timetrap/timer.rb', line 78

def running?
  !!active_entry
end

#running_entriesObject



92
93
94
# File 'lib/timetrap/timer.rb', line 92

def running_entries
  Entry.filter(:end => nil)
end

#safe_for_time_parse?(string) ⇒ Boolean

Time.parse is optimistic and will parse things like ‘=18’ into midnight on 18th of this month. It will also turn ‘total garbage’ into Time.now Here we do some sanity checks on the string to protect it from common cli formatting issues, and allow reasonable warning to be passed back to the user.

Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/timetrap/timer.rb', line 45

def safe_for_time_parse?(string)
  # misformatted cli option
  !string.include?('=') and
  # a date time string needs a number in it
  string =~ /\d/
end

#start(note, time = nil) ⇒ Object

Raises:



119
120
121
122
123
# File 'lib/timetrap/timer.rb', line 119

def start note, time = nil
  raise AlreadyRunning if running?
  time ||= Time.now
  Entry.create(:sheet => Timer.current_sheet, :note => note, :start => time).save
end

#stop(sheet_or_entry, time = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/timetrap/timer.rb', line 100

def stop sheet_or_entry, time = nil
  a = case sheet_or_entry
  when Entry
    sheet_or_entry
  when String
    active_entry(sheet_or_entry)
  end

  if a
    time ||= Time.now
    a.end = time
    a.save
    meta = Meta.find(:key => 'last_checkout_id') || Meta.create(:key => 'last_checkout_id')
    meta.value = a.id
    meta.save
  end
  a
end

#stop_all(time = nil) ⇒ Object



96
97
98
# File 'lib/timetrap/timer.rb', line 96

def stop_all(time = nil)
  running_entries.map{ |e| stop(e, time) }
end

#time_closest_to_now_with_chronic(time, now) ⇒ Object



32
33
34
35
36
37
# File 'lib/timetrap/timer.rb', line 32

def time_closest_to_now_with_chronic(time, now)
  [
    Chronic.parse(time, :context => :past, :now => now),
    Chronic.parse(time, :context => :future, :now => now)
  ].sort_by{|a| (a.to_i - now.to_i).abs }.first
end