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