Module: Timeless::Stopwatch

Defined in:
lib/timeless/stopwatch.rb

Constant Summary collapse

START_FILENAME =

Storing and retrieving start time and notes

file containing the start time

File.expand_path("~/.timeless-tmp.csv")

Class Method Summary collapse

Class Method Details

.clocking?Boolean



37
38
39
# File 'lib/timeless/stopwatch.rb', line 37

def self.clocking?
  File.exists?(START_FILENAME) 
end

.forgetObject

forget the start clock



57
58
59
# File 'lib/timeless/stopwatch.rb', line 57

def self.forget
  File.delete(START_FILENAME) if File.exists?(START_FILENAME) 
end

.get_startObject



41
42
43
44
45
46
47
48
# File 'lib/timeless/stopwatch.rb', line 41

def self.get_start
  if clocking?
    array = CSV.read(START_FILENAME, "r")
    [array[0][0], array[0][1]]
  else
    [nil, nil]
  end
end

.start(start = nil, notes = nil) ⇒ Object



8
9
10
# File 'lib/timeless/stopwatch.rb', line 8

def self.start start=nil, notes=nil
  store_start (start ? start : Time.now), (notes ? notes : "")
end

.stop(start = nil, stop = nil, notes = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/timeless/stopwatch.rb', line 12

def self.stop start=nil, stop=nil, notes=nil
  if not start
    start, _ = get_start
  end

  if notes == nil or notes == ""
    _, notes = get_start
  end

  if not stop
    stop = Time.now
  end

  forget # forget started clock, if any

  [start, stop, notes]
end

.store_start(start, notes) ⇒ Object



50
51
52
53
54
# File 'lib/timeless/stopwatch.rb', line 50

def self.store_start start, notes
  CSV.open(START_FILENAME, "w") do |csv|
    csv << [start, notes]
  end
end