Code Coverage
 
Lines
Covered
43.01% covered (danger)
43.01%
80 / 186
1
require 'set'
1
module TheFox
1
module Timr
1
module Command
# - Print informations about a specific [Task](rdoc-ref:TheFox::Timr::Model::Task).
# - Add/remove a Task.
# - Edit (set) a Task.
#
# Man page: [timr-task(1)](../../../../man/timr-task.1.html)
1
class TaskCommand < BasicCommand
1
include TheFox::Timr::Model
1
include TheFox::Timr::Helper
1
include TheFox::Timr::Error
# Path to man page.
1
MAN_PATH = 'man/timr-task.1'
1
def initialize(argv = Array.new)
20
super()
# puts "argv #{argv}"
20
@help_opt = false
20
@show_opt = false
20
@add_opt = false
20
@remove_opt = false
20
@set_opt = false
20
@verbose_opt = false
20
@tracks_opt = false
20
@foreign_id_opt = nil
20
@unset_foreign_id_opt = nil
20
@name_opt = nil
20
@description_opt = nil
20
@estimation_opt = nil
20
@billed_opt = nil
20
@unbilled_opt = nil
20
@hourly_rate_opt = nil
20
@unset_hourly_rate_opt = nil
20
@has_flat_rate_opt = nil
20
@unset_flat_rate_opt = nil
# Holds Task instances.
20
@tasks_opt = Set.new
20
loop_c = 0 # Limit the loop.
20
while loop_c < 1024 && argv.length > 0
25
loop_c += 1
25
arg = argv.shift
25
case arg
when '-h', '--help'
1
@help_opt = true
when '-v', '--verbose'
0
@verbose_opt = true
when '-t', '--tracks'
1
@tracks_opt = true
when '--id'
1
@foreign_id_opt = argv.shift.strip
when '--no-id'
1
@unset_foreign_id_opt = true
when '-n', '--name'
1
@name_opt = argv.shift
when '--desc', '--description', '-d' # -d not official
1
@description_opt = argv.shift
when '-e', '--est', '--estimation'
1
@estimation_opt = argv.shift
# puts "est: #{@estimation_opt}"
when '-b', '--billed'
2
@billed_opt = true
when '--unbilled'
2
@unbilled_opt = true
when '-r', '--hourly-rate'
1
@hourly_rate_opt = argv.shift
when '--no-hourly-rate'
1
@unset_hourly_rate_opt = true
when '--fr', '--flat', '--flat-rate'
1
@has_flat_rate_opt = true
when '--no-flat', '--no-flat-rate'
1
@unset_flat_rate_opt = true
when 'show'
1
@show_opt = true
when 'add'
1
@add_opt = true
when 'remove'
1
@remove_opt = true
when 'set'
3
@set_opt = true
when Task
0
@tasks_opt << arg
else
4
@tasks_opt << arg
end
# puts "argv #{argv}"
end
20
if @foreign_id_opt && @unset_foreign_id_opt
1
raise TaskCommandError, 'Cannot use --id and --no-id.'
end
19
if @billed_opt && @unbilled_opt
2
raise TaskCommandError, 'Cannot use --billed and --unbilled.'
end
17
if !@hourly_rate_opt.nil? && @unset_hourly_rate_opt
1
raise TaskCommandError, 'Cannot use --hourly-rate and --no-hourly-rate.'
end
16
if @has_flat_rate_opt && @unset_flat_rate_opt
1
raise TaskCommandError, 'Cannot use --flat-rate and --no-flat-rate.'
end
end
# See BasicCommand#run.
1
def run
2
if @help_opt
0
help
0
return
end
2
@timr = Timr.new(@cwd)
2
if @add_opt
0
run_add
2
elsif @remove_opt
0
run_remove
2
elsif @set_opt
2
run_set
else
0
if @tasks_opt.count == 0
0
run_show_all
else
0
run_show
end
end
end
1
private
# Uses TheFox::Timr::Timr.add_task.
1
def run_add
0
options = {
:foreign_id => @foreign_id_opt,
:name => @name_opt,
:description => @description_opt,
:estimation => @estimation_opt,
}
0
task = @timr.add_task(options)
0
puts task.to_detailed_str
end
1
def run_remove
0
@tasks_opt.each do |task_id|
0
task = @timr.remove_task({:task_id => task_id})
0
tracks_s = TranslationHelper.pluralize(@timr.stack.tracks.count, 'track', 'tracks')
0
puts 'Deleted Task %s (%s).' % [task.short_id, tracks_s]
end
end
1
def run_set
2
if @tasks_opt.count == 0
1
raise TaskCommandError, 'No Task ID given.'
end
1
task_id = @tasks_opt.first
1
if @foreign_id_opt.nil? && @unset_foreign_id_opt.nil? && \
@name_opt.nil? && @description_opt.nil? && \
@estimation_opt.nil? && \
@billed_opt.nil? && @unbilled_opt.nil? && \
@hourly_rate_opt.nil? && @unset_hourly_rate_opt.nil? && \
@has_flat_rate_opt.nil? && @unset_flat_rate_opt.nil?
1
raise TaskCommandError, "No option given. See 'timr task -h'."
end
0
task = @timr.get_task_by_id(task_id)
0
puts '--- OLD ---'
0
puts task.to_detailed_str
0
puts
0
if @foreign_id_opt && task.foreign_id != @foreign_id_opt
0
@timr.foreign_id_db.remove_task(task)
# Throws exception when Foreign ID already exists in DB.
# Break before Task save.
0
@timr.foreign_id_db.add_task(task, @foreign_id_opt)
0
@timr.foreign_id_db.save_to_file
end
0
if @unset_foreign_id_opt
0
@timr.foreign_id_db.remove_task(task)
0
@timr.foreign_id_db.save_to_file
end
0
if @name_opt
0
task.name = @name_opt
end
0
if @description_opt
0
task.description = @description_opt
end
0
if @estimation_opt
0
task.estimation = @estimation_opt
end
0
if @billed_opt || @unbilled_opt
0
if @billed_opt
0
task.is_billed = true
else
0
task.is_billed = false
end
end
0
if @hourly_rate_opt
0
task.hourly_rate = @hourly_rate_opt
end
0
if @unset_hourly_rate_opt
0
task.hourly_rate = nil
end
0
if @has_flat_rate_opt
0
task.has_flat_rate = true
end
0
if @unset_flat_rate_opt
0
task.has_flat_rate = false
end
0
task.save_to_file
0
puts '--- NEW ---'
0
puts task.to_detailed_str
end
1
def run_show
0
options = Hash.new
0
if @verbose_opt
0
options[:full_id] = true
end
0
tasks = Array.new
0
@tasks_opt.each do |task_id_or_instance|
0
if task_id_or_instance.is_a?(Task)
0
task = task_id_or_instance
else
0
task = @timr.get_task_by_id(task_id_or_instance)
end
0
tasks << task.to_detailed_array(options)
end
0
if tasks.count > 0
0
puts tasks.map{ |t| t.join("\n") }.join("\n\n")
end
end
1
def run_show_all
0
@timr.tasks.each do |task_id, task|
0
task_id = @verbose_opt ? task.id : task.short_id
0
if task.foreign_id
0
puts '%s %s %s' % [task_id, task.foreign_id, task.name_s]
else
0
puts '%s - %s' % [task_id, task.name_s]
end
end
end
1
def help
0
puts 'usage: timr task [show] [[-t|--tracks] <id>|<task_id>...]'
0
puts ' or: timr task add [--id <str>] [-n|--name <name>] [--description <str>]'
0
puts ' [--estimation <time>] [--billed|--unbilled]'
0
puts ' [--hourly-rate <float>] [--no-hourly-rate]'
0
puts ' [--flat-rate|--no-flat-rate]'
0
puts ' or: timr task set [--id <str>] [-n|--name <name>] [--description <str>]'
0
puts ' [--estimation <time>] [--billed|--unbilled]'
0
puts ' [--hourly-rate <float>] [--no-hourly-rate]'
0
puts ' [--flat-rate|--no-flat-rate]'
0
puts ' <id>|<task_id>'
0
puts ' or: timr task remove <id>|<task_id>...'
0
puts ' or: timr task [-h|--help]'
0
puts
0
puts 'Subcommands'
0
puts ' show Default command. When no Task ID is given print all Tasks.'
0
puts ' add Add a new Task without starting it.'
0
puts ' remove Remove an existing Task.'
0
puts ' set Edit an existing Task.'
0
puts
0
puts 'Show Options'
0
puts ' -t, --tracks Show a list of Track IDs for each Task.'
0
puts
0
puts 'Add/Set Options'
0
puts ' --id <str> Your ID to identify the Task.'
0
puts ' -n, --name <name> Task Name.'
0
puts ' --desc, --description <str> Task Description.'
0
puts ' -e, --est, --estimation <time> Task Estimation. See details below.'
0
puts ' -b, --billed Mark Task as billed.'
0
puts ' --unbilled Mark Task as unbilled.'
0
puts ' -r, --hourly-rate <float> Set the Hourly Rate.'
0
puts ' --no-hourly-rate Unset Hourly Rate.'
0
puts ' --fr, --flat-rate, --flat Has Task a Flat Rate?'
0
puts ' --no-flat-rate, --no-flat Unset Flat Rate.'
0
puts
0
HelpCommand.print_man_units_help
0
puts
0
HelpCommand.print_estimation_help(true)
0
puts
0
puts 'Billed/Unbilled'
0
puts ' If a whole Task gets billed/unbilled all Tracks are changed to'
0
puts " billed/unbilled. Each Track has a flag 'is_billed'."
0
puts
end
end # class TaskCommand
end # module Command
end # module Timr
end # module TheFox