Class: TimeTrackr::Database

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Database

Returns a new instance of Database.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/timetrackr/database.rb', line 15

def initialize(options={})
  @options = options
  begin
    require 'yaml'
    @log_path = options['path']
    if !File.exist? @log_path
      @db = {:current => [], :tasks => {}}
      write_file
    end
    @db = YAML.load_file(@log_path)
    puts 'Loaded database' if options['verbose']
  rescue LoadError
    puts 'YAML not found'
  end
end

Instance Method Details

#clear(task) ⇒ Object

clear an task



95
96
97
98
# File 'lib/timetrackr/database.rb', line 95

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

#closeObject

cleanup and close



88
89
90
# File 'lib/timetrackr/database.rb', line 88

def close
  write_file
end

#currentObject

return an array of current tasks



34
35
36
# File 'lib/timetrackr/database.rb', line 34

def current
  @db[:current]
end

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

get task history as an array of Periods



69
70
71
72
73
# File 'lib/timetrackr/database.rb', line 69

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

rename a task



78
79
80
81
82
83
# File 'lib/timetrackr/database.rb', line 78

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

start a period with optional notes



48
49
50
51
52
53
54
# File 'lib/timetrackr/database.rb', line 48

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

stop a period



59
60
61
62
63
64
# File 'lib/timetrackr/database.rb', line 59

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

#tasksObject

return an array of all tasks



41
42
43
# File 'lib/timetrackr/database.rb', line 41

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