Skip to content

Instantly share code, notes, and snippets.

@renaehodgkins
Created December 8, 2008 04:40
Show Gist options
  • Save renaehodgkins/33344 to your computer and use it in GitHub Desktop.
Save renaehodgkins/33344 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -wKU
require 'yaml'
class TimeTrack
attr_accessor :projects
FILENAME = 'timelog.yml'
def initialize
prepare_data_file
read_data
project_name = get_project_name
new_session = time_track
puts project_name
puts new_session
modify_data(project_name, new_session)
puts @projects.inspect
write_data
end
def modify_data(project_name, session)
if @projects[project_name]
@projects[project_name]["log"].push(session)
@projects[project_name]["total"] = time_format(total_time(@projects[project_name]["log"]))
else
@projects.merge!({project_name => {"total" => time_format(session), "log" => [session]}})
end
end
def total_time(log)
log.inject(0) {|sum, time| sum = sum + time}
end
def time_format(time)
Time.at(time).gmtime.strftime('%H:%M:%S')
end
def write_data
open(FILENAME, 'w') {|f| f << @projects.to_yaml}
end
def time_start
puts "Please hit Enter to start time tracking"
if gets.chomp == ""
Time.now
else
time_get
end
end
def time_stop
puts "Please hit Enter again to stop time tracking"
if gets.chomp == ""
Time.now
else
time_stop
end
end
def time_track
start_time = time_start
puts "Time tracking started at #{start_time}"
end_time = time_stop
puts "Time tracking ended at #{end_time}"
end_time - start_time
end
def get_project_name
puts "Please enter a name for this project"
gets.chomp
end
private
def read_data
@projects = YAML.load(open(FILENAME))
@projects = {} unless @projects
end
def prepare_data_file
if !File.exist?(FILENAME)
File.new(FILENAME, 'w')
end
end
end
TimeTrack.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment