Module: Timeless::Storage

Defined in:
lib/timeless/storage.rb

Constant Summary collapse

TIMELESS_FILE =
File.expand_path("~/.timeless.csv")

Class Method Summary collapse

Class Method Details

.exportObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/timeless/storage.rb', line 32

def self.export
  entries = CSV.read(TIMELESS_FILE)

  CSV { |csvout| csvout << ["Start Date", "Start Time", "End Date", "End Time", "Duration (s)", "Project", "Notes"] }
  CSV do |csvout|
    entries.each do |entry|
      start = Time.parse(entry[0])
      stop = Time.parse(entry[1])
      
      start_date = start.strftime("%Y-%m-%d")
      start_time = start.strftime("%H:%M:%S")
      
      stop_date = stop.strftime("%Y-%m-%d")
      stop_time = stop.strftime("%H:%M:%S")
      
      duration = stop - start
    
      # extract project and client, if present
      project = extract_kpv "p", entry[2]
      client = extract_kpv "c", entry[2]

      notes = entry[2]

      csvout << [start_date, start_time, stop_date, stop_time, duration, project, client, notes]
    end
  end
end

.get(from_date = nil, to_date = nil, filter = nil) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/timeless/storage.rb', line 17

def self.get from_date=nil, to_date=nil, filter=nil
  entries = CSV.read(TIMELESS_FILE)
  entries.select do |x|
    (from_date ? Time.parse(x[0]) >= from_date : true) and
      (to_date ? Time.parse(x[1]) <= to_date : true) and
      (filter ? x[2].include?(filter) : true)
  end
end

.get_key(key) ⇒ Object

get all unique keys of a given type in file



27
28
29
30
# File 'lib/timeless/storage.rb', line 27

def self.get_key key
  entries = CSV.read(TIMELESS_FILE)
  entries.map { |x| extract_kpv key, x[2] }.uniq.sort
end

.lastObject



13
14
15
# File 'lib/timeless/storage.rb', line 13

def self.last
  CSV.read(TIMELESS_FILE).last
end

.store(start, stop, notes) ⇒ Object



7
8
9
10
11
# File 'lib/timeless/storage.rb', line 7

def self.store start, stop, notes
  CSV.open(TIMELESS_FILE, "a") do |csv|
    csv << [start, stop, notes]
  end
end