Class: ThinService::Command::Base
- Inherits:
-
Object
- Object
- ThinService::Command::Base
- Defined in:
- lib/thin_service/command.rb
Direct Known Subclasses
Commands::Install, Commands::Installdaemon, Commands::Remove
Instance Attribute Summary collapse
-
#done_validating ⇒ Object
readonly
Returns the value of attribute done_validating.
-
#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 = {}) ⇒ Base
constructor
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.
-
#validate ⇒ Object
Returns true/false depending on whether the command is configured properly.
Constructor Details
#initialize(options = {}) ⇒ Base
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.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/thin_service/command.rb', line 39 def initialize(={}) argv = [:argv] || [] @opt = OptionParser.new @opt. = ThinService::Command::BANNER @valid = true @done_validating = false configure @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 puts("Version #{ThinService::VERSION}") end @opt.parse! argv end |
Instance Attribute Details
#done_validating ⇒ Object (readonly)
Returns the value of attribute done_validating.
23 24 25 |
# File 'lib/thin_service/command.rb', line 23 def done_validating @done_validating end |
#valid ⇒ Object (readonly)
Returns the value of attribute valid.
23 24 25 |
# File 'lib/thin_service/command.rb', line 23 def valid @valid end |
Instance Method Details
#configure ⇒ Object
62 63 64 |
# File 'lib/thin_service/command.rb', line 62 def configure [] end |
#failure(message) ⇒ Object
Just a simple method to display failure until something better is developed.
109 110 111 |
# File 'lib/thin_service/command.rb', line 109 def failure() STDERR.puts "!!! #{message}" end |
#help ⇒ Object
Returns a help message. Defaults to OptionParser#help which should be good.
72 73 74 |
# File 'lib/thin_service/command.rb', line 72 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.
28 29 30 31 32 33 34 35 |
# File 'lib/thin_service/command.rb', line 28 def (opts) 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.
78 79 80 |
# File 'lib/thin_service/command.rb', line 78 def run raise NotImplementedError end |
#valid?(exp, message) ⇒ Boolean
Validates the given expression is true and prints the message if not, exiting.
84 85 86 87 88 89 90 |
# File 'lib/thin_service/command.rb', line 84 def valid?(exp, ) if !@done_validating && !exp failure @valid = false @done_validating = true end end |
#valid_dir?(file, message) ⇒ Boolean
Validates that the given directory exists
104 105 106 |
# File 'lib/thin_service/command.rb', line 104 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
93 94 95 |
# File 'lib/thin_service/command.rb', line 93 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.
99 100 101 |
# File 'lib/thin_service/command.rb', line 99 def valid_file?(file, ) valid?(file != nil && File.file?(file), ) end |
#validate ⇒ Object
Returns true/false depending on whether the command is configured properly.
67 68 69 |
# File 'lib/thin_service/command.rb', line 67 def validate return @valid end |