A Command pattern implementation used to create the set of command available to the user from Mongrel. The script uses objects which implement this interface to do the user’s bidding.
Called by the subclass to setup the command and parse the argv arguments. The call is destructive on argv since it uses the OptionParser#parse! function.
# File lib/mongrel/command.rb, line 44 44: def initialize(options={}) 45: argv = options[:argv] || [] 46: @opt = OptionParser.new 47: @opt.banner = Mongrel::Command::BANNER 48: @valid = true 49: # this is retarded, but it has to be done this way because -h and -v exit 50: @done_validating = false 51: @original_args = argv.dup 52: 53: configure 54: 55: # I need to add my own -h definition to prevent the -h by default from exiting. 56: @opt.on_tail("-h", "--help", "Show this message") do 57: @done_validating = true 58: puts @opt 59: end 60: 61: # I need to add my own -v definition to prevent the -v from exiting by default as well. 62: @opt.on_tail("--version", "Show version") do 63: @done_validating = true 64: puts "Version #{Mongrel::Const::MONGREL_VERSION}" 65: end 66: 67: @opt.parse! argv 68: end
# File lib/mongrel/command.rb, line 70 70: def configure 71: options [] 72: end
Just a simple method to display failure until something better is developed.
# File lib/mongrel/command.rb, line 137 137: def failure(message) 138: STDERR.puts "!!! #{message}" 139: end
Returns a help message. Defaults to OptionParser#help which should be good.
# File lib/mongrel/command.rb, line 80 80: def help 81: @opt.help 82: end
Called by the implemented command to set the options for that command. Every option has a short and long version, a description, a variable to set, and a default value. No exceptions.
# File lib/mongrel/command.rb, line 32 32: def options(opts) 33: # process the given options array 34: opts.each do |short, long, help, variable, default| 35: self.instance_variable_set(variable, default) 36: @opt.on(short, long, help) do |arg| 37: self.instance_variable_set(variable, arg) 38: end 39: end 40: end
Runs the command doing it’s job. You should implement this otherwise it will throw a NotImplementedError as a reminder.
# File lib/mongrel/command.rb, line 86 86: def run 87: raise NotImplementedError 88: end
Validates the given expression is true and prints the message if not, exiting.
# File lib/mongrel/command.rb, line 92 92: def valid?(exp, message) 93: if not @done_validating and (not exp) 94: failure message 95: @valid = false 96: @done_validating = true 97: end 98: end
Validates that the given directory exists
# File lib/mongrel/command.rb, line 112 112: def valid_dir?(file, message) 113: valid?(file != nil && File.directory?(file), message) 114: end
Validates that a file exists and if not displays the message
# File lib/mongrel/command.rb, line 101 101: def valid_exists?(file, message) 102: valid?(file != nil && File.exist?(file), message) 103: end
Validates that the file is a file and not a directory or something else.
# File lib/mongrel/command.rb, line 107 107: def valid_file?(file, message) 108: valid?(file != nil && File.file?(file), message) 109: end
# File lib/mongrel/command.rb, line 126 126: def valid_group?(group) 127: valid?(@user, "You must also specify a user.") 128: begin 129: Etc.getgrnam(group) 130: rescue 131: failure "Group does not exist: #{group}" 132: @valid = false 133: end 134: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.