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: if VERSION 65: puts "Version #{Mongrel::Const::MONGREL_VERSION}" 66: end 67: end 68: 69: @opt.parse! argv 70: end
# File lib/mongrel/command.rb, line 72 72: def configure 73: options [] 74: end
Just a simple method to display failure until something better is developed.
# File lib/mongrel/command.rb, line 139 139: def failure(message) 140: STDERR.puts "!!! #{message}" 141: end
Returns a help message. Defaults to OptionParser#help which should be good.
# File lib/mongrel/command.rb, line 82 82: def help 83: @opt.help 84: 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 88 88: def run 89: raise NotImplementedError 90: end
Validates the given expression is true and prints the message if not, exiting.
# File lib/mongrel/command.rb, line 94 94: def valid?(exp, message) 95: if not @done_validating and (not exp) 96: failure message 97: @valid = false 98: @done_validating = true 99: end 100: end
Validates that the given directory exists
# File lib/mongrel/command.rb, line 114 114: def valid_dir?(file, message) 115: valid?(file != nil && File.directory?(file), message) 116: end
Validates that a file exists and if not displays the message
# File lib/mongrel/command.rb, line 103 103: def valid_exists?(file, message) 104: valid?(file != nil && File.exist?(file), message) 105: end
Validates that the file is a file and not a directory or something else.
# File lib/mongrel/command.rb, line 109 109: def valid_file?(file, message) 110: valid?(file != nil && File.file?(file), message) 111: end
# File lib/mongrel/command.rb, line 128 128: def valid_group?(group) 129: valid?(@user, "You must also specify a user.") 130: begin 131: Etc.getgrnam(group) 132: rescue 133: failure "Group does not exist: #{group}" 134: @valid = false 135: end 136: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.