Class: TimeTrackr::YamlDatabase

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

Instance Method Summary collapse

Methods inherited from Database

create, #time

Constructor Details

#initialize(path) ⇒ YamlDatabase

Returns a new instance of YamlDatabase.



16
17
18
19
20
21
22
23
# File 'lib/timetrackr/yaml.rb', line 16

def initialize(path)
  @log_path = path
  if !File.exist? @log_path
    @db = {:current => [], :tasks => {}}
    write_file
  end
  @db = YAML.load_file(@log_path)
end

Instance Method Details

#clear(task) ⇒ Object



65
66
67
68
# File 'lib/timetrackr/yaml.rb', line 65

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

#closeObject



61
62
63
# File 'lib/timetrackr/yaml.rb', line 61

def close
  write_file
end

#currentObject



25
26
27
# File 'lib/timetrackr/yaml.rb', line 25

def current
  @db[:current]
end

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



48
49
50
51
52
# File 'lib/timetrackr/yaml.rb', line 48

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



54
55
56
57
58
59
# File 'lib/timetrackr/yaml.rb', line 54

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



33
34
35
36
37
38
39
# File 'lib/timetrackr/yaml.rb', line 33

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



41
42
43
44
45
46
# File 'lib/timetrackr/yaml.rb', line 41

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

#tasksObject



29
30
31
# File 'lib/timetrackr/yaml.rb', line 29

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