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

valid[R]
done_validating[R]
original_args[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
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

Public Instance Methods

configure() click to toggle source
    # File lib/mongrel/command.rb, line 70
70:       def configure
71:         options []
72:       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 137
137:       def failure(message)
138:         STDERR.puts "!!! #{message}"
139:       end
help() click to toggle source

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
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
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() 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 86
86:       def run
87:         raise NotImplementedError
88:       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 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
valid_dir?(file, message) click to toggle source

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
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 101
101:       def valid_exists?(file, message)
102:         valid?(file != nil && File.exist?(file), message)
103:       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 107
107:       def valid_file?(file, message)
108:         valid?(file != nil && File.file?(file), message)
109:       end
valid_group?(group) click to toggle source
     # 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
valid_user?(user) click to toggle source
     # File lib/mongrel/command.rb, line 116
116:       def valid_user?(user)
117:         valid?(@group, "You must also specify a group.")
118:         begin
119:           Etc.getpwnam(user)
120:         rescue
121:           failure "User does not exist: #{user}"
122:           @valid = false
123:         end
124:       end
validate() click to toggle source

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

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

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.