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

.balance(from, to, filter) ⇒ Object



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

def self.balance from, to, filter
  entries = Timeless::Storage.get(from, to, filter)

  hash = {"p" => {}, "c" => {}, "a" => {}}

  entries.each do |entry|
    start = Time.parse(entry[0])
    stop = Time.parse(entry[1])
    duration = (stop - start) / 60

    project = extract_kpv "p", entry[2]
    client = extract_kpv "c", entry[2]
    activity = extract_kpv "a", entry[2]

    hash["p"][project] = (hash["p"][project] || 0) + duration if project != ""
    hash["c"][client] = (hash["c"][client] || 0) + duration if client != ""
    hash["a"][activity] = (hash["a"][activity] || 0) + duration if activity != ""

  end
  hash
end

.exportObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/timeless/storage.rb', line 55

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

  CSV { |csvout| csvout << ["Start Date", "Start Time", "End Date", "End Time", "Duration (s)", "Project", "Client", "Activity", "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]
      activity = extract_kpv "a", entry[2]

      notes = entry[2]

      csvout << [start_date, start_time, stop_date, stop_time, duration, project, client, activity, 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).sort { |x, y| x[1] <=> y[1] }.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