summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/timetrackr.rb22
-rw-r--r--lib/timetrackr/period.rb21
-rw-r--r--lib/timetrackr/yaml.rb57
3 files changed, 64 insertions, 36 deletions
diff --git a/lib/timetrackr.rb b/lib/timetrackr.rb
index 75bf520..c93da9b 100644
--- a/lib/timetrackr.rb
+++ b/lib/timetrackr.rb
@@ -34,6 +34,20 @@ class TimeTrackr
end
#
+ # start a period
+ #
+ def start(task,notes)
+ raise 'Not implemented'
+ end
+
+ #
+ # stop a period
+ #
+ def stop(task)
+ raise 'Not implemented'
+ end
+
+ #
# return an array of all tasks
#
def tasks
@@ -42,21 +56,19 @@ class TimeTrackr
#
# time in task in seconds
- # only considers 'start' and 'stop' events
#
def time(task)
- raise 'Not Implemented'
end
#
- # write an event
+ # get task history as an array of Periods
#
- def event(task, time=Time.now, details={})
+ def history(task)
raise 'Not Implemented'
end
#
- # clear an event
+ # clear an task
#
def clear(task)
raise 'Not Implemented'
diff --git a/lib/timetrackr/period.rb b/lib/timetrackr/period.rb
new file mode 100644
index 0000000..ce83c82
--- /dev/null
+++ b/lib/timetrackr/period.rb
@@ -0,0 +1,21 @@
+class Period
+
+ attr_accessor :task, :start, :stop, :notes
+
+ def initialize(task, start, stop=nil, notes='')
+ @task = task
+ @start = start
+ @stop = stop
+ @notes = notes
+ end
+
+ def length
+ stop = @stop || Time.now
+ stop - @start
+ end
+
+ def current?
+ stop.nil?
+ end
+
+end
diff --git a/lib/timetrackr/yaml.rb b/lib/timetrackr/yaml.rb
index ac5e299..f3547bc 100644
--- a/lib/timetrackr/yaml.rb
+++ b/lib/timetrackr/yaml.rb
@@ -1,3 +1,15 @@
+#
+# keeps the following format in a yaml file:
+#
+# :current: []
+#
+# :tasks:
+# foo:
+# - :start: 2011-05-16 14:26:26.263449 +07:00
+# :stop: 2011-05-16 14:26:27 +07:00
+# :notes: "blah blah blah"
+#
+
class YamlTimeTrackr < TimeTrackr
def initialize(path)
@@ -18,33 +30,25 @@ class YamlTimeTrackr < TimeTrackr
@db[:tasks].keys.compact.uniq || []
end
- def time(task)
- total = 0 # seconds
- split = nil
- @db[:tasks][task].sort{|x,y| x[:time] <=> y[:time]}.each do |event|
- if event[:action] == 'start'
- split = event[:time]
- end
- if split && (event[:action] == 'stop')
- total = total + (event[:time] - split)
- split = nil
- end
+ 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
- total = (total + (Time.now - split)) if split
- total.to_i
end
- def event(task='default', time=Time.now, details={})
- details[:action] ||= 'start'
- details[:time] = time
- if details[:action].to_s == 'stop' && @db[:current].delete(task)
- write_event(task,details)
+ def stop(task)
+ if @db[:current].include?(task)
+ @db[:current].delete(task)
+ @db[:tasks][task].last[:stop] = Time.now
end
+ end
- if details[:action].to_s == 'start' && !@db[:current].include?(task)
- @db[:current].unshift(task)
- write_event(task,details)
- end
+ 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
def close
@@ -58,15 +62,6 @@ class YamlTimeTrackr < TimeTrackr
private
- def write_event(task, details)
- @db[:tasks] = {} unless @db[:tasks]
- if @db[:tasks][task]
- @db[:tasks][task].push(details)
- else
- @db[:tasks][task] = Array[details]
- end
- end
-
def write_file
File.open(@log_path,'w') do |fh|
YAML.dump(@db,fh)