module Timetrap::Timer
Public Instance Methods
active_entry(sheet=nil)
click to toggle source
# File lib/timetrap/timer.rb, line 93 def active_entry(sheet=nil) Entry.find(:sheet => (sheet || Timer.current_sheet), :end => nil) end
auto_sheet()
click to toggle source
# File lib/timetrap/timer.rb, line 136 def auto_sheet if Timetrap::Config['auto_sheet'] load_auto_sheet(Config['auto_sheet']).new.sheet end end
current_sheet()
click to toggle source
# File lib/timetrap/timer.rb, line 64 def current_sheet unless Meta.find(:key => 'current_sheet') Meta.create(:key => 'current_sheet', :value => 'default') end if the_auto_sheet = auto_sheet unless @auto_sheet_warned warn "Sheet #{the_auto_sheet.inspect} selected by Timetrap::AutoSheets::#{::Timetrap::Config['auto_sheet'].capitalize}" @auto_sheet_warned = true end the_auto_sheet else Meta.find(:key => 'current_sheet').value end end
current_sheet=(sheet)
click to toggle source
# File lib/timetrap/timer.rb, line 54 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)
click to toggle source
# File lib/timetrap/timer.rb, line 85 def entries sheet = nil Entry.filter(:sheet => sheet).order_by(:start) end
last_checkout()
click to toggle source
the last entry to be checked out of
# File lib/timetrap/timer.rb, line 98 def last_checkout meta = Meta.find(:key => 'last_checkout_id') Entry[meta.value] if meta end
last_sheet()
click to toggle source
# File lib/timetrap/timer.rb, line 80 def last_sheet m = Meta.find(:key => 'last_sheet') m and m.value end
process_time(time, now = Time.now)
click to toggle source
# File lib/timetrap/timer.rb, line 13 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?()
click to toggle source
# File lib/timetrap/timer.rb, line 89 def running? !!active_entry end
running_entries()
click to toggle source
# File lib/timetrap/timer.rb, line 103 def running_entries Entry.filter(:end => nil) end
safe_for_time_parse?(string)
click to toggle source
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.
# File lib/timetrap/timer.rb, line 47 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)
click to toggle source
# File lib/timetrap/timer.rb, line 130 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)
click to toggle source
# File lib/timetrap/timer.rb, line 111 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)
click to toggle source
# File lib/timetrap/timer.rb, line 107 def stop_all(time = nil) running_entries.map{ |e| stop(e, time) } end
time_closest_to_now_with_chronic(time, now)
click to toggle source
# File lib/timetrap/timer.rb, line 34 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