Class: Mongrel::Command::Registry

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mongrel/command.rb

Overview

A Singleton class that manages all of the available commands and handles running them.

Instance Method Summary collapse

Instance Method Details

#commandsObject

Builds a list of possible commands from the Command derivates list



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/mongrel/command.rb', line 129

def commands
  list = Command.derivatives()
  match = Regexp.new("(.*::.*)|(.*command.*)", Regexp::IGNORECASE)
  
  results = []
  list.keys.each do |key|
    results << key.to_s unless match.match(key.to_s)
  end
  
  return results.sort
end

Prints a list of available commands.



142
143
144
145
146
147
148
149
150
151
# File 'lib/mongrel/command.rb', line 142

def print_command_list
  puts "Available commands are:\n\n"
  
  self.commands.each do |name|
    puts " - #{name}\n"
  end
  
  puts "\nEach command takes -h as an option to get help."
  
end

#run(args) ⇒ Object

Runs the args against the first argument as the command name. If it has any errors it returns a false, otherwise it return true.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/mongrel/command.rb', line 156

def run(args)
  # find the command and change the program's name to reflect it
  cmd_name = args.shift
  $0 = "#{cmd_name}"
  
  if cmd_name == "?" or cmd_name == "help"
    print_command_list
    return true
  end
  
  # command exists, set it up and validate it
  begin
    command = Command.create(cmd_name, args)
  rescue FactoryError
    STDERR.puts "INVALID COMMAND: #$!"
    print_command_list
    return
  end
  
  # Normally the command is NOT valid right after being created
  # but sometimes (like with -h or -v) there's no further processing
  # needed so the command is already valid so we can skip it.
  if not command.done_validating
    if not command.validate
      STDERR.puts "#{cmd_name} reported an error. Use -h to get help."
      return false
    else
      command.run
    end
  end
  return true
end