Module: Timetrap::Timer
Defined Under Namespace
Classes: AlreadyRunning
Instance Method Summary collapse
- #active_entry(sheet = nil) ⇒ Object
- #current_sheet ⇒ Object
- #current_sheet=(sheet) ⇒ Object
- #entries(sheet = nil) ⇒ Object
-
#last_checkout ⇒ Object
the last entry to be checked out of.
- #last_sheet ⇒ Object
- #process_time(time, now = Time.now) ⇒ Object
- #running? ⇒ Boolean
- #running_entries ⇒ Object
-
#safe_for_time_parse?(string) ⇒ Boolean
Time.parse is optimistic and will parse things like ‘=18’ into midnight on 18th of this month.
- #start(note, time = nil) ⇒ Object
- #stop(sheet_or_entry, time = nil) ⇒ Object
- #stop_all(time = nil) ⇒ Object
- #time_closest_to_now_with_chronic(time, now) ⇒ Object
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_sheet ⇒ Object
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_checkout ⇒ Object
the last entry to be checked out of
87 88 89 90 |
# File 'lib/timetrap/timer.rb', line 87 def last_checkout = Meta.find(:key => 'last_checkout_id') Entry[.value] if end |
#last_sheet ⇒ Object
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
78 79 80 |
# File 'lib/timetrap/timer.rb', line 78 def running? !!active_entry end |
#running_entries ⇒ Object
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.
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
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.find(:key => 'last_checkout_id') || Meta.create(:key => 'last_checkout_id') .value = a.id .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 |