Class: TheFox::Timr::Command::BasicCommand

Inherits:
Object
  • Object
show all
Extended by:
Error
Includes:
Error, Helper
Defined in:
lib/timr/command/basic_command.rb

Overview

Basic Class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = Array.new) ⇒ BasicCommand

Returns a new instance of BasicCommand.



17
18
19
20
# File 'lib/timr/command/basic_command.rb', line 17

def initialize(argv = Array.new)
  @cwd = nil
  @timr = nil
end

Instance Attribute Details

#cwdObject

Current Working Directory



15
16
17
# File 'lib/timr/command/basic_command.rb', line 15

def cwd
  @cwd
end

Class Method Details

.create_command_from_argv(argv) ⇒ Object

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`.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Object

Get the Class for each command string.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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

Instance Method Details

#runObject

This is the actual execution of the Command.

Raises:

  • (NotImplementedError)


23
24
25
# File 'lib/timr/command/basic_command.rb', line 23

def run
  raise NotImplementedError
end

#shutdownObject

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



28
29
30
31
32
# File 'lib/timr/command/basic_command.rb', line 28

def shutdown
  if @timr
    @timr.shutdown
  end
end