Class: Mongrel::Command::Command

Inherits:
Object
  • Object
show all
Includes:
PluginFactory
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.

Creating a new command is very easy, and you can do it without modifying the source of Mongrel thanks to PluginFactory. What you do is the following:

1.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PluginFactory

#create, #derivativeClasses, #derivatives, extend_object, #factoryType, #getModuleName, #getSubclass, included, #inherited, #loadDerivative, log, #makeRequirePath, #requireDerivative

Constructor Details

#initialize(argv) ⇒ Command

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.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mongrel/command.rb', line 42

def initialize(argv)
  @opt = OptionParser.new
  @valid = true
  # this is retarded, but it has to be done this way because -h and -v exit
  @done_validating = false

  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 -h from exiting by default as well.
  @opt.on_tail("--version", "Show version") do
    @done_validating = true
    if VERSION
      puts "Version #{VERSION}"
    end
  end
  
  @opt.parse! argv
end

Instance Attribute Details

#done_validatingObject (readonly)

Returns the value of attribute done_validating.



25
26
27
# File 'lib/mongrel/command.rb', line 25

def done_validating
  @done_validating
end

#validObject (readonly)

Returns the value of attribute valid.



25
26
27
# File 'lib/mongrel/command.rb', line 25

def valid
  @valid
end

Class Method Details

.derivativeDirsObject

Tells the PluginFactory where to look for additional commands. By default it’s just a “plugins” directory wherever we are located.



69
70
71
# File 'lib/mongrel/command.rb', line 69

def self.derivativeDirs
  return ["plugins"]
end

Instance Method Details

#failure(message) ⇒ Object

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



116
117
118
# File 'lib/mongrel/command.rb', line 116

def failure(message)
  STDERR.puts "!!! #{message}"
end

#helpObject

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



79
80
81
# File 'lib/mongrel/command.rb', line 79

def help
  @opt.help
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.



30
31
32
33
34
35
36
37
38
# File 'lib/mongrel/command.rb', line 30

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

#runObject

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

Raises:

  • (NotImplementedError)


85
86
87
# File 'lib/mongrel/command.rb', line 85

def run
  raise NotImplementedError
end

#valid?(exp, message) ⇒ Boolean

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

Returns:

  • (Boolean)


91
92
93
94
95
96
97
# File 'lib/mongrel/command.rb', line 91

def valid?(exp, message)
  if not @done_validating and (not exp)
    failure message
    @valid = false
    @done_validating = true
  end
end

#valid_dir?(file, message) ⇒ Boolean

Validates that the given directory exists

Returns:

  • (Boolean)


111
112
113
# File 'lib/mongrel/command.rb', line 111

def valid_dir?(file, message)
  valid?(file != nil && File.directory?(file), message)
end

#valid_exists?(file, message) ⇒ Boolean

Validates that a file exists and if not displays the message

Returns:

  • (Boolean)


100
101
102
# File 'lib/mongrel/command.rb', line 100

def valid_exists?(file, message)
  valid?(file != nil && File.exist?(file), message)
end

#valid_file?(file, message) ⇒ Boolean

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

Returns:

  • (Boolean)


106
107
108
# File 'lib/mongrel/command.rb', line 106

def valid_file?(file, message)
  valid?(file != nil && File.file?(file), message)
end

#validateObject

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



74
75
76
# File 'lib/mongrel/command.rb', line 74

def validate
  return @valid
end