Module: Timetrap::Timer

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

Defined Under Namespace

Classes: AlreadyRunning

Instance Method Summary collapse

Methods included from Helpers::AutoLoad

auto_load, load_auto_sheet, load_formatter

Instance Method Details

#active_entry(sheet = nil) ⇒ Object



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

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

#auto_sheetObject



136
137
138
139
140
# 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_sheetObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



54
55
56
57
58
59
60
61
62
# 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) ⇒ Object



85
86
87
# File 'lib/timetrap/timer.rb', line 85

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

#last_checkoutObject

the last entry to be checked out of



98
99
100
101
# File 'lib/timetrap/timer.rb', line 98

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

#last_sheetObject



80
81
82
83
# 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) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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?Boolean

Returns:

  • (Boolean)


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

def running?
  !!active_entry
end

#running_entriesObject



103
104
105
# File 'lib/timetrap/timer.rb', line 103

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)


47
48
49
50
51
52
# 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) ⇒ Object

Raises:



130
131
132
133
134
# 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) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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) ⇒ Object



107
108
109
# 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) ⇒ Object



34
35
36
37
38
39
# 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