Class: TimeTrackr::JsonDatabase

Inherits:
Database
  • Object
show all
Defined in:
lib/timetrackr/json.rb

Instance Method Summary collapse

Methods inherited from Database

create, #time

Constructor Details

#initialize(path) ⇒ JsonDatabase

Returns a new instance of JsonDatabase.



4
5
6
7
8
9
10
11
12
13
# File 'lib/timetrackr/json.rb', line 4

def initialize(path)
  @log_path = path
  if !File.exist? @log_path
    @db = {'current' => [], 'tasks' => {}}
    write_file
  end
  File.open(@log_path,'r') do |fh|
    @db = JSON.load(fh)
  end
end

Instance Method Details

#clear(task) ⇒ Object



55
56
57
58
# File 'lib/timetrackr/json.rb', line 55

def clear(task)
  @db['current'].delete(task)
  @db['tasks'].delete(task)
end

#closeObject



51
52
53
# File 'lib/timetrackr/json.rb', line 51

def close
  write_file
end

#currentObject



15
16
17
# File 'lib/timetrackr/json.rb', line 15

def current
  @db['current']
end

#history(task, p_begin = nil, p_end = nil) ⇒ Object



38
39
40
41
42
# File 'lib/timetrackr/json.rb', line 38

def history(task, p_begin=nil, p_end=nil)
  @db['tasks'][task].sort{|x,y| x['start'] <=> y['start']}.collect {|p|
    Period.new(task,p['start'],p['stop'],p['notes'])
  } unless !@db['tasks'].include? task
end

#rename(from, to) ⇒ Object



44
45
46
47
48
49
# File 'lib/timetrackr/json.rb', line 44

def rename(from, to)
  @db['tasks'][to] = @db['tasks'].delete(from)
  if @db['current'].delete(from)
    @db['current'].unshift(to)
  end
end

#start(task, notes) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/timetrackr/json.rb', line 23

def start(task, notes)
  @db['tasks'][task] = Array[] unless @db['tasks'][task]
  if !@db['current'].include?(task)
    @db['current'].unshift(task)
    @db['tasks'][task].push({'start' => Time.now, 'notes' => notes})
  end
end

#stop(task) ⇒ Object



31
32
33
34
35
36
# File 'lib/timetrackr/json.rb', line 31

def stop(task)
  if @db['current'].include?(task)
    @db['current'].delete(task)
    @db['tasks'][task].last['stop'] = Time.now
  end
end

#tasksObject



19
20
21
# File 'lib/timetrackr/json.rb', line 19

def tasks
  @db['tasks'].keys.compact.uniq || []
end