Code Coverage
 
Lines
Covered
26.03% covered (danger)
26.03%
57 / 219
1
require 'set'
1
module TheFox
1
module Timr
1
module Command
# - Print informations about a specific [Track](rdoc-ref:TheFox::Timr::Model::Track).
# - Add/remove a Track.
# - Edit (set) a Track.
#
# Man page: [timr-track(1)](../../../../man/timr-track.1.html)
1
class TrackCommand < BasicCommand
1
include TheFox::Timr::Model
1
include TheFox::Timr::Error
# Path to man page.
1
MAN_PATH = 'man/timr-track.1'
1
def initialize(argv = Array.new)
9
super()
9
@help_opt = false
9
@show_opt = false
9
@add_opt = false
9
@remove_opt = false
9
@set_opt = false
9
@verbose_opt = false
9
@tracks_opt = Set.new
9
@task_opt = false
9
@task_id_opt = nil
9
@message_opt = nil
9
@start_date_opt = nil
9
@start_time_opt = nil
9
@end_date_opt = nil
9
@end_time_opt = nil
9
@billed_opt = nil
9
@unbilled_opt = nil
9
loop_c = 0 # Limit the loop.
9
while loop_c < 1024 && argv.length > 0
11
loop_c += 1
11
arg = argv.shift
11
case arg
when '-h', '--help'
1
@help_opt = true
when '-v', '--verbose'
0
@verbose_opt = true
when '-t', '--task'
2
if @set_opt
1
@task_id_opt = argv.shift
else
1
@task_opt = true
end
when '-m', '--message'
1
@message_opt = argv.shift
when '--sd', '--start-date'
0
@start_date_opt = argv.shift
when '--st', '--start-time'
0
@start_time_opt = argv.shift
when '--ed', '--end-date'
0
@end_date_opt = argv.shift
when '--et', '--end-time'
0
@end_time_opt = argv.shift
when '-b', '--billed'
1
@billed_opt = true
when '--unbilled'
1
@unbilled_opt = true
when 'show'
1
@show_opt = true
when 'add'
1
@add_opt = true
when 'remove'
1
@remove_opt = true
when 'set'
1
@set_opt = true
else
1
@tracks_opt << arg
end
end
9
if @billed_opt && @unbilled_opt
1
raise TrackCommandError, 'Cannot use --billed and --unbilled.'
end
end
# See BasicCommand#run.
1
def run
0
if @help_opt
0
help
0
return
end
0
@timr = Timr.new(@cwd)
0
@timr.load_all_tracks
0
if @task_opt
0
run_task_command
0
elsif @add_opt
0
run_add
0
elsif @remove_opt
0
run_remove
0
elsif @set_opt
0
run_set
else
0
run_show
end
end
1
private
1
def run_task_command
0
tasks = Hash.new # Set.new
0
@tracks_opt.each do |track_id|
0
track = @timr.get_track_by_id(track_id)
0
task = track.task
0
unless tasks.has_key?(task.id)
0
tasks[task.id] = task
end
end
0
task_command_args = tasks.values
0
if @verbose_opt
0
task_command_args << '--verbose'
end
0
task_command = TaskCommand.new(task_command_args)
0
task_command.run
end
1
def run_show
0
options = Hash.new
0
if @verbose_opt
0
options[:full_id] = true
end
0
tracks = Array.new
0
@tracks_opt.each do |track_id|
0
track = @timr.get_track_by_id(track_id)
0
unless track
0
raise TrackCommandError, "Track for ID '#{track_id}' not found."
end
0
tracks << track.to_detailed_array(options)
end
0
if tracks.count > 0
0
puts tracks.map{ |track_s| track_s.join("\n") }.join("\n\n")
end
end
1
def run_add
0
task_id = check_task_id
0
check_opts_add
0
task = @timr.get_task_by_id(task_id)
0
track = Track.new
0
if @start_date_opt && @start_time_opt
0
track.begin_datetime = Time.parse("#{@start_date_opt} #{@start_time_opt}")
end
0
if @end_date_opt && @end_time_opt
0
track.end_datetime = Time.parse("#{@end_date_opt} #{@end_time_opt}")
end
0
if @message_opt
0
track.message = @message_opt
end
0
if @billed_opt || @unbilled_opt
0
if @billed_opt
0
track.is_billed = true
else
0
track.is_billed = false
end
end
0
task.add_track(track)
0
task.save_to_file
0
puts track.to_detailed_str
end
1
def run_remove
0
@tracks_opt.each do |track_id|
0
task_track_h = @timr.remove_track({:track_id => track_id})
0
task = task_track_h[:task]
0
track = task_track_h[:track]
0
puts 'Deleted Track %s from Task %s.' % [track.short_id, task.short_id]
end
end
1
def run_set
0
track_id = check_track_id
0
check_opts_set
0
track = @timr.get_track_by_id(track_id)
0
unless track
0
raise TrackError, "Track for ID '#{track_id}' not found."
end
0
puts '--- OLD ---'
0
puts track.to_detailed_str
0
puts
0
bdt = track.begin_datetime
0
edt = track.end_datetime
# Start DateTime
0
if @start_date_opt && @start_time_opt
0
track.begin_datetime = "#{@start_date_opt}T#{@start_time_opt}"
0
elsif @start_date_opt.nil? && @start_time_opt
0
if bdt
0
track.begin_datetime = "#{bdt.strftime('%F')}T#{@start_time_opt}"
else
0
raise TrackCommandError, 'Start Date must be given. Track has no Start DateTime.'
end
0
elsif @start_date_opt && @start_time_opt.nil?
0
if bdt
0
track.begin_datetime = "#{@start_date_opt}T#{bdt.strftime('%T%z')}"
else
0
raise TrackCommandError, 'Start Time must be given. Track has no Start DateTime.'
end
# elsif @start_date_opt.nil? && @start_time_opt.nil?
# raise TrackCommandError, 'Start Date or Start Time must be given.'
end
# End DateTime
0
if @end_date_opt && @end_time_opt
0
track.end_datetime = "#{@end_date_opt}T#{@end_time_opt}"
0
elsif @end_date_opt.nil? && @end_time_opt
0
if edt
0
track.end_datetime = "#{edt.strftime('%F')}T#{@end_time_opt}"
else
0
raise TrackCommandError, 'End Date must be given. Track has no End DateTime.'
end
0
elsif @end_date_opt && @end_time_opt.nil?
0
if edt
0
track.end_datetime = "#{@end_date_opt}T#{edt.strftime('%T%z')}"
else
0
raise TrackCommandError, 'End Time must be given. Track has no End DateTime.'
end
# elsif @end_date_opt.nil? && @end_time_opt.nil?
# raise TrackCommandError, 'End Date or End Time must be given.'
end
# Message
0
if @message_opt
0
track.message = @message_opt
end
# Billed / Unbilled
0
if @billed_opt || @unbilled_opt
0
if @billed_opt
0
track.is_billed = true
else
0
track.is_billed = false
end
end
0
task = track.task
0
if @task_id_opt
0
target_task = @timr.get_task_by_id(@task_id_opt)
0
task.move_track(track, target_task)
0
target_task.save_to_file
0
if @timr.stack.on_stack?(track)
0
@timr.stack.changed
0
@timr.stack.save_to_file
end
end
0
task.save_to_file
0
puts '--- NEW ---'
0
puts track.to_detailed_str
end
1
def check_task_id
0
if @tracks_opt.count == 0
0
raise TrackCommandError, 'No Task ID given.'
end
0
@tracks_opt.first
end
1
def check_track_id
0
if @tracks_opt.count == 0
0
raise TrackCommandError, 'No Track ID given.'
end
0
@tracks_opt.first
end
1
def check_opts_add
0
if @start_date_opt || @start_time_opt || \
@end_date_opt || @end_time_opt
0
if @start_date_opt.nil?
0
raise TrackCommandError, 'Start Date must be given.'
end
0
if @start_time_opt.nil?
0
raise TrackCommandError, 'Start Time must be given.'
end
0
if @end_date_opt.nil?
0
raise TrackCommandError, 'End Date must be given.'
end
0
if @end_time_opt.nil?
0
raise TrackCommandError, 'End Time must be given.'
end
end
end
1
def check_opts_set
0
if @start_date_opt.nil? && @start_time_opt.nil? && \
@end_date_opt.nil? && @end_time_opt.nil? && \
@message_opt.nil? && @task_id_opt.nil? && \
@billed_opt.nil? && @unbilled_opt.nil?
0
raise TrackCommandError, "No option given. See 'timr track --help'."
end
end
1
def help
0
puts 'usage: timr track [show] [-t|--task] <track_id>...'
0
puts ' or: timr track add [-m|--message <message>]'
0
puts ' [--start-date <date> --start-time <time>'
0
puts ' [--end-date <date> --end-time <time>]]'
0
puts ' [--billed|--unbilled] <id>|<task_id>'
0
puts ' or: timr track set [-m|--message <message>]'
0
puts ' [--start-date <date> --start-time <time>]'
0
puts ' [--end-date <date> --end-time <time>]'
0
puts ' [-t|--task <id>|<task_id>] [--billed|--unbilled]'
0
puts ' <track_id>'
0
puts ' or: timr track remove <track_id>...'
0
puts ' or: timr track [-h|--help]'
0
puts
0
puts 'Show Options'
0
puts " -t, --task Show Task of Track. Same as 'timr task <task_id>'."
0
puts
0
puts 'Add/Set Options'
0
puts ' -m, --message <message> Track Message. What have you done?'
0
puts ' --sd, --start-date <date> Start Date'
0
puts ' --st, --start-time <time> Start Time'
0
puts ' --ed, --end-date <date> End Date'
0
puts ' --et, --end-time <time> End Time'
0
puts ' -b, --billed Mark Track as billed.'
0
puts ' --unbilled Mark Track as unbilled.'
0
puts
0
puts 'Set Options'
0
puts ' --task <id>|<task_id> Move Track to another Task.'
0
puts
0
puts 'Start DateTime must be given when End DateTime is given. A Track cannot have a'
0
puts 'End DateTime without a Start DateTime.'
0
puts
0
HelpCommand.print_man_units_help
0
puts
0
HelpCommand.print_datetime_help
0
puts
end
end # class TrackCommand
end # module Command
end # module Timr
end # module TheFox