class TheFox::Timr::Command::BasicCommand

Basic Class

Attributes

cwd[RW]

Current Working Directory

Public Class Methods

create_command_from_argv(argv) click to toggle source

Creates a new Command instance for each command string.

For example, it returns a new StopCommand instance when `stop` String is provided by `argv` Array.

Primary used by `bin/timr`.

# File lib/timr/command/basic_command.rb, line 44
def create_command_from_argv(argv)
        # -C <path>
        cwd_opt = Pathname.new("#{Dir.home}/.timr/defaultc").expand_path # Default Client
        
        command_name = nil
        command_argv = Array.new
        loop_c = 0
        while loop_c < 1024 && argv.length > 0
                loop_c += 1
                arg = argv.shift
                
                if command_name
                        command_argv << arg
                else
                        case arg
                        when '-h', '--help', 'help'
                                command_name = 'help'
                        when '-V', '--version'
                                command_name = 'version'
                        when '-C'
                                cwd_opt = Pathname.new(argv.shift).expand_path
                        when '--install-basepath'
                                timr_gem = Gem::Specification.find_by_name('timr')
                                print timr_gem.gem_dir
                                exit
                        else
                                if arg[0] == '-'
                                        raise CommandError, "Unknown argument '#{arg}'. See 'timr --help'."
                                else
                                        command_name = arg
                                end
                        end
                end
        end
        
        command_class = get_command_class_by_name(command_name)
        command = command_class.new(command_argv)
        command.cwd = cwd_opt
        command
end
get_command_class_by_name(name) click to toggle source

Get the Class for each command string.

# File lib/timr/command/basic_command.rb, line 86
def get_command_class_by_name(name)
        case name
        when 'help', '', nil
                command = HelpCommand
        when 'version'
                command = VersionCommand
        
        when 'status', 's'
                command = StatusCommand
        when 'start'
                command = StartCommand
        when 'stop'
                command = StopCommand
        when 'push'
                command = PushCommand
        when 'pop'
                command = PopCommand
        when 'continue', 'cont', 'c'
                command = ContinueCommand
        when 'pause', 'p'
                command = PauseCommand
        when 'log'
                command = LogCommand
        when 'task'
                command = TaskCommand
        when 'track'
                command = TrackCommand
        when 'report'
                command = ReportCommand
        when 'reset'
                command = ResetCommand
        else
                raise CommandError, "'%s' is not a timr command. See 'timr --help'." % [name]
        end
end
new(argv = Array.new) click to toggle source
# File lib/timr/command/basic_command.rb, line 17
def initialize(argv = Array.new)
        @cwd = nil
        @timr = nil
end

Public Instance Methods

run() click to toggle source

This is the actual execution of the Command.

# File lib/timr/command/basic_command.rb, line 23
def run
        raise NotImplementedError
end
shutdown() click to toggle source

Should be executed after `run` to gently save everything.

# File lib/timr/command/basic_command.rb, line 28
def shutdown
        if @timr
                @timr.shutdown
        end
end

Private Instance Methods

check_foreign_id(foreign_id) click to toggle source
# File lib/timr/command/basic_command.rb, line 160
def check_foreign_id(foreign_id)
        if foreign_id && foreign_id.match(/ /)
                foreign_id_without_spaces = foreign_id.gsub(' ', '')
                
                raise ForeignIdError, "Foreign ID (--id) cannot include spaces. Maybe you want to use '#{foreign_id_without_spaces}' instead."
        end
end
run_edit(task_id = nil, track_id = nil) click to toggle source

Uses

@timr
@edit_opt
@task_id_opt
@track_id_opt
# File lib/timr/command/basic_command.rb, line 131
def run_edit(task_id = nil, track_id = nil)
        task_id ||= @task_id_opt
        track_id ||= @track_id_opt
        
        if @timr && @edit_opt
                edit_text = Array.new
                
                if @message_opt
                        edit_text << @message_opt.clone
                else
                        track = @timr.get_track_by_task_id(task_id, track_id)
                        
                        if track && track.message
                                edit_text << track.message.clone
                        else
                                edit_text << @message_opt.clone
                        end
                end
                
                TerminalHelper.external_editor_help(edit_text)
                
                editor_message = TerminalHelper.run_external_editor(edit_text)
                
                if editor_message.length > 0
                        @message_opt = editor_message
                end
        end
end