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
- #running? ⇒ Boolean
- #running_entries ⇒ Object
- #start(note, time = nil) ⇒ Object
- #stop(sheet, time = nil) ⇒ Object
Instance Method Details
#active_entry(sheet = nil) ⇒ Object
32 33 34 |
# File 'lib/timetrap/timer.rb', line 32 def active_entry(sheet=nil) Entry.find(:sheet => (sheet || Timer.current_sheet), :end => nil) end |
#current_sheet ⇒ Object
17 18 19 20 21 22 |
# File 'lib/timetrap/timer.rb', line 17 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
11 12 13 14 15 |
# File 'lib/timetrap/timer.rb', line 11 def current_sheet= sheet m = Meta.find_or_create(:key => 'current_sheet') m.value = sheet m.save end |
#entries(sheet = nil) ⇒ Object
24 25 26 |
# File 'lib/timetrap/timer.rb', line 24 def entries sheet = nil Entry.filter(:sheet => sheet).order_by(:start) end |
#running? ⇒ Boolean
28 29 30 |
# File 'lib/timetrap/timer.rb', line 28 def running? !!active_entry end |
#running_entries ⇒ Object
36 37 38 |
# File 'lib/timetrap/timer.rb', line 36 def running_entries Entry.filter(:end => nil) end |
#start(note, time = nil) ⇒ Object
48 49 50 51 52 |
# File 'lib/timetrap/timer.rb', line 48 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, time = nil) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/timetrap/timer.rb', line 40 def stop sheet, time = nil if a = active_entry(sheet) time ||= Time.now a.end = time a.save end end |