Created
December 2, 2008 02:50
-
-
Save renaehodgkins/30956 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -wKU | |
require 'yaml' | |
class TimeTrack | |
def initialize(project_name) | |
@project_name = project_name | |
@time_list = project_filename | |
prepare_file(@time_list) | |
all_times = YAML.load(open(@time_list)) | |
all_times = [] unless all_times | |
all_times << time_track | |
open(@time_list, 'w') {|f| f << all_times.to_yaml} | |
add_times | |
end | |
def time_get | |
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_get | |
puts "Time tracking started at #{@start_time}" | |
@end_time = time_stop | |
puts "Time tracking ended at #{@end_time}" | |
@end_time - @start_time | |
end | |
def time_format(time) | |
Time.at(time).gmtime.strftime('%H:%M:%S') | |
end | |
def add_times | |
total_filename = "#{@time_list}" | |
totaled_times = YAML.load(open(@time_list)) | |
total = totaled_times.inject(0) {|sum, time| sum = sum + time} | |
open(project_total_filename, 'w') {|f| f << time_format(total)} | |
end | |
private | |
def project_total_filename | |
@project_name.downcase.gsub(" ", "_") + "_total.yml" | |
end | |
def project_filename | |
@project_name.downcase.gsub(" ", "_") + ".yml" | |
end | |
def prepare_file(filename) | |
if !File.exist?(filename) | |
File.new(filename, 'w') | |
end | |
end | |
end | |
class Project | |
def get_project_name | |
puts "Please enter a name for this project" | |
@project_name = gets.chomp | |
puts "#{@project_name} has been created." | |
puts "Do you want to start tracking time for this project?" | |
puts "Please answer yes or no" | |
if gets.chomp == "yes" | |
TimeTrack.new(@project_name) | |
end | |
end | |
end | |
project = Project.new | |
project.get_project_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment