lib/detom/commands/record.rb in detom-0.0.1 vs lib/detom/commands/record.rb in detom-0.0.2

- old
+ new

@@ -1,35 +1,55 @@ module Commands class Record - def initialize(store) + CLIENT_REQUIRED_MESSAGE = "Cannot log time without a client. Either provide <client> to detom record, or configure a value for client in this directory using detom set." + DATE_FORMAT_MESSAGE = "Day/month is an unrecognised format. Use `%d-%m` format" + TIME_TO_LOG_FORMAT_MESSAGE = "Time must be provided in minutes or hours, e,g 40m or 3h" + + def initialize(store, local_config) @store = store + @local_config = local_config end - def call(client_name, time_to_log, day_month = nil) - if day_month - raise "Day/month is an unrecognised format. Use `%d-%m` format" unless day_month =~ /\d\d-\d\d/ + def call(time_to_log, client_name=nil, day_month=nil) + client = client_for client_name - day = day_month.split("-").first - month = day_month.split("-").last - - # parse - day_month = [Time.now.year.to_s, month, day].join "-" - else - day_month = Time.now.strftime("%Y-%m-%d") - end + date_stamp = format(day_month) + + client[date_stamp] = [] unless client[date_stamp] + client[date_stamp] << minutes_from(time_to_log) + + @store.save! + $stdout.puts "Logged #{time_to_log} for #{client_name}" + end + + private + def format(day_month) + return Time.now.strftime("%Y-%m-%d") if day_month.nil? + + raise DATE_FORMAT_MESSAGE unless day_month =~ /\d\d-\d\d/ + + splits = day_month.split "-" + [Time.now.year.to_s, splits.last, splits.first].join "-" + end + + def client_for(client_name) + raise CLIENT_REQUIRED_MESSAGE if client_name.nil? && @local_config.client.nil? + + client_name = client_name || @local_config.client + @store[client_name] = {} if @store[client_name].nil? - client = @store[client_name] + @store[client_name] + end - if client[day_month] - client[day_month] << time_to_log.to_i - else - client[day_month] = [ time_to_log.to_i ] + def minutes_from(time_to_log) + multiplier = case time_to_log + when /\d+m/ then 1 + when /\d+h/ then 60 + else raise TIME_TO_LOG_FORMAT_MESSAGE end - @store.save! - puts "Logged #{time_to_log} for #{client_name}" + multiplier * time_to_log.to_i end end end -