Module: Mongrel::Command::Base
- Defined in:
- lib/mongrel/command.rb
Overview
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.
Instance Attribute Summary collapse
-
#done_validating ⇒ Object
readonly
Returns the value of attribute done_validating.
-
#original_args ⇒ Object
readonly
Returns the value of attribute original_args.
-
#valid ⇒ Object
readonly
Returns the value of attribute valid.
Instance Method Summary collapse
- #configure ⇒ Object
-
#failure(message) ⇒ Object
Just a simple method to display failure until something better is developed.
-
#help ⇒ Object
Returns a help message.
-
#initialize(options = {}) ⇒ Object
Called by the subclass to setup the command and parse the argv arguments.
-
#options(opts) ⇒ Object
Called by the implemented command to set the options for that command.
-
#run ⇒ Object
Runs the command doing it’s job.
-
#valid?(exp, message) ⇒ Boolean
Validates the given expression is true and prints the message if not, exiting.
-
#valid_dir?(file, message) ⇒ Boolean
Validates that the given directory exists.
-
#valid_exists?(file, message) ⇒ Boolean
Validates that a file exists and if not displays the message.
-
#valid_file?(file, message) ⇒ Boolean
Validates that the file is a file and not a directory or something else.
- #valid_group?(group) ⇒ Boolean
- #valid_user?(user) ⇒ Boolean
-
#validate ⇒ Object
Returns true/false depending on whether the command is configured properly.
Instance Attribute Details
#done_validating ⇒ Object (readonly)
Returns the value of attribute done_validating.
27 28 29 |
# File 'lib/mongrel/command.rb', line 27 def done_validating @done_validating end |
#original_args ⇒ Object (readonly)
Returns the value of attribute original_args.
27 28 29 |
# File 'lib/mongrel/command.rb', line 27 def original_args @original_args end |
#valid ⇒ Object (readonly)
Returns the value of attribute valid.
27 28 29 |
# File 'lib/mongrel/command.rb', line 27 def valid @valid end |
Instance Method Details
#configure ⇒ Object
72 73 74 |
# File 'lib/mongrel/command.rb', line 72 def configure [] end |
#failure(message) ⇒ Object
Just a simple method to display failure until something better is developed.
139 140 141 |
# File 'lib/mongrel/command.rb', line 139 def failure() STDERR.puts "!!! #{}" end |
#help ⇒ Object
Returns a help message. Defaults to OptionParser#help which should be good.
82 83 84 |
# File 'lib/mongrel/command.rb', line 82 def help @opt.help end |
#initialize(options = {}) ⇒ Object
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.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/mongrel/command.rb', line 44 def initialize(={}) argv = [:argv] || [] @opt = OptionParser.new @opt. = 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 |
#options(opts) ⇒ Object
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.
32 33 34 35 36 37 38 39 40 |
# File 'lib/mongrel/command.rb', line 32 def (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 ⇒ Object
Runs the command doing it’s job. You should implement this otherwise it will throw a NotImplementedError as a reminder.
88 89 90 |
# File 'lib/mongrel/command.rb', line 88 def run raise NotImplementedError end |
#valid?(exp, message) ⇒ Boolean
Validates the given expression is true and prints the message if not, exiting.
94 95 96 97 98 99 100 |
# File 'lib/mongrel/command.rb', line 94 def valid?(exp, ) if not @done_validating and (not exp) failure @valid = false @done_validating = true end end |
#valid_dir?(file, message) ⇒ Boolean
Validates that the given directory exists
114 115 116 |
# File 'lib/mongrel/command.rb', line 114 def valid_dir?(file, ) valid?(file != nil && File.directory?(file), ) end |
#valid_exists?(file, message) ⇒ Boolean
Validates that a file exists and if not displays the message
103 104 105 |
# File 'lib/mongrel/command.rb', line 103 def valid_exists?(file, ) valid?(file != nil && File.exist?(file), ) end |
#valid_file?(file, message) ⇒ Boolean
Validates that the file is a file and not a directory or something else.
109 110 111 |
# File 'lib/mongrel/command.rb', line 109 def valid_file?(file, ) valid?(file != nil && File.file?(file), ) end |
#valid_group?(group) ⇒ Boolean
128 129 130 131 132 133 134 135 136 |
# 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) ⇒ Boolean
118 119 120 121 122 123 124 125 126 |
# 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 ⇒ Object
Returns true/false depending on whether the command is configured properly.
77 78 79 |
# File 'lib/mongrel/command.rb', line 77 def validate return @valid end |