module Mongrel::Command::Base

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.

Attributes

done_validating[R]
original_args[R]
valid[R]

Public Class Methods

new(options={}) click to toggle source

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
def initialize(options={})
  argv = options[:argv] || []
  @opt = OptionParser.new
  @opt.banner = Mongrel::Command::BANNER
  @valid = true
  # this is retarded, but it has to be done this way because -h and -v exit
  @done_validating = false
  @original_args = argv.dup

  configure

  # I need to add my own -h definition to prevent the -h by default from exiting.
  @opt.on_tail("-h", "--help", "Show this message") do
    @done_validating = true
    puts @opt
  end

  # I need to add my own -v definition to prevent the -v from exiting by default as well.
  @opt.on_tail("--version", "Show version") do
    @done_validating = true
    if VERSION
      puts "Version #{Mongrel::Const::MONGREL_VERSION}"
    end
  end

  @opt.parse! argv
end

Public Instance Methods

configure() click to toggle source
# File lib/mongrel/command.rb, line 72
def configure
  options []
end
failure(message) click to toggle source

Just a simple method to display failure until something better is developed.

# File lib/mongrel/command.rb, line 139
def failure(message)
  STDERR.puts "!!! #{message}"
end
help() click to toggle source

Returns a help message. Defaults to OptionParser#help which should be good.

# File lib/mongrel/command.rb, line 82
def help
  @opt.help
end
options(opts) click to toggle source

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
def options(opts)
  # process the given options array
  opts.each do |short, long, help, variable, default|
    self.instance_variable_set(variable, default)
    @opt.on(short, long, help) do |arg|
      self.instance_variable_set(variable, arg)
    end
  end
end
run() click to toggle source

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
def run
  raise NotImplementedError
end
valid?(exp, message) click to toggle source

Validates the given expression is true and prints the message if not, exiting.

# File lib/mongrel/command.rb, line 94
def valid?(exp, message)
  if not @done_validating and (not exp)
    failure message
    @valid = false
    @done_validating = true
  end
end
valid_dir?(file, message) click to toggle source

Validates that the given directory exists

# File lib/mongrel/command.rb, line 114
def valid_dir?(file, message)
  valid?(file != nil && File.directory?(file), message)
end
valid_exists?(file, message) click to toggle source

Validates that a file exists and if not displays the message

# File lib/mongrel/command.rb, line 103
def valid_exists?(file, message)
  valid?(file != nil && File.exist?(file), message)
end
valid_file?(file, message) click to toggle source

Validates that the file is a file and not a directory or something else.

# File lib/mongrel/command.rb, line 109
def valid_file?(file, message)
  valid?(file != nil && File.file?(file), message)
end
valid_group?(group) click to toggle source
# File lib/mongrel/command.rb, line 128
def valid_group?(group)
  valid?(@user, "You must also specify a user.")
  begin
    Etc.getgrnam(group)
  rescue
    failure "Group does not exist: #{group}"
    @valid = false
  end
end
valid_user?(user) click to toggle source
# File lib/mongrel/command.rb, line 118
def valid_user?(user)
  valid?(@group, "You must also specify a group.")
  begin
    Etc.getpwnam(user)
  rescue
    failure "User does not exist: #{user}"
    @valid = false
  end
end
validate() click to toggle source

Returns true/false depending on whether the command is configured properly.

# File lib/mongrel/command.rb, line 77
def validate
  return @valid
end