Module: Mongrel::Command::Base

Module | Mongrel::Command::Base
In: | lib/mongrel/command.rb lib/mongrel/command.rb

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.

### Methods

configure configure failure failure help help new new options options run run valid? valid? valid_dir? valid_dir? valid_exists? valid_exists? valid_file? valid_file? valid_group? valid_group? valid_user? valid_user? validate validate

### Attributes

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

### Public Class methods

new(options={})

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.

[Source]

        # 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

new(options={})

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.

[Source]

        # 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

### Public Instance methods

configure()

[Source]

        # File lib/mongrel/command.rb, line 72
    72:       def configure
    73:         options []
    74:       end

configure()

[Source]

        # File lib/mongrel/command.rb, line 72
    72:       def configure
    73:         options []
    74:       end

failure(message)

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

[Source]

         # File lib/mongrel/command.rb, line 139
    139:       def failure(message)
    140:         STDERR.puts "!!! #{message}"
    141:       end

failure(message)

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

[Source]

         # File lib/mongrel/command.rb, line 139
    139:       def failure(message)
    140:         STDERR.puts "!!! #{message}"
    141:       end

help()

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

[Source]

        # File lib/mongrel/command.rb, line 82
    82:       def help
    83:         @opt.help
    84:       end

help()

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

[Source]

        # File lib/mongrel/command.rb, line 82
    82:       def help
    83:         @opt.help
    84:       end

options(opts)

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.

[Source]

        # 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

options(opts)

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.

[Source]

        # 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

run()

Runs the command doing it‘s job. You should implement this otherwise it will throw a NotImplementedError as a reminder.

[Source]

        # File lib/mongrel/command.rb, line 88
    88:       def run
    89:         raise NotImplementedError
    90:       end

run()

Runs the command doing it‘s job. You should implement this otherwise it will throw a NotImplementedError as a reminder.

[Source]

        # File lib/mongrel/command.rb, line 88
    88:       def run
    89:         raise NotImplementedError
    90:       end

valid?(exp, message)

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

[Source]

         # 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

valid?(exp, message)

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

[Source]

         # 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

valid_dir?(file, message)

Validates that the given directory exists

[Source]

         # File lib/mongrel/command.rb, line 114
    114:       def valid_dir?(file, message)
    115:         valid?(file != nil && File.directory?(file), message)
    116:       end

valid_dir?(file, message)

Validates that the given directory exists

[Source]

         # File lib/mongrel/command.rb, line 114
    114:       def valid_dir?(file, message)
    115:         valid?(file != nil && File.directory?(file), message)
    116:       end

valid_exists?(file, message)

Validates that a file exists and if not displays the message

[Source]

         # File lib/mongrel/command.rb, line 103
    103:       def valid_exists?(file, message)
    104:         valid?(file != nil && File.exist?(file), message)
    105:       end

valid_exists?(file, message)

Validates that a file exists and if not displays the message

[Source]

         # File lib/mongrel/command.rb, line 103
    103:       def valid_exists?(file, message)
    104:         valid?(file != nil && File.exist?(file), message)
    105:       end

valid_file?(file, message)

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

[Source]

         # File lib/mongrel/command.rb, line 109
    109:       def valid_file?(file, message)
    110:         valid?(file != nil && File.file?(file), message)
    111:       end

valid_file?(file, message)

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

[Source]

         # File lib/mongrel/command.rb, line 109
    109:       def valid_file?(file, message)
    110:         valid?(file != nil && File.file?(file), message)
    111:       end

valid_group?(group)

[Source]

         # 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

valid_group?(group)

[Source]

         # 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

valid_user?(user)

[Source]

         # File lib/mongrel/command.rb, line 118
    118:       def valid_user?(user)
    119:         valid?(@group, "You must also specify a group.")
    120:         begin
    121:           Etc.getpwnam(user)
    122:         rescue
    123:           failure "User does not exist: #{user}"
    124:           @valid = false
    125:         end
    126:       end

valid_user?(user)

[Source]

         # File lib/mongrel/command.rb, line 118
    118:       def valid_user?(user)
    119:         valid?(@group, "You must also specify a group.")
    120:         begin
    121:           Etc.getpwnam(user)
    122:         rescue
    123:           failure "User does not exist: #{user}"
    124:           @valid = false
    125:         end
    126:       end

validate()

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

[Source]

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

validate()

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

[Source]

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

[Validate]