class WhirledPeas::Command::Base

Abstract base class for all commands

Attributes

args[R]
config[R]

Public Class Methods

command_name() click to toggle source

Returns the name of the command as expected by the command line script. By convention, this name is snake case version of the class name, e.g. the command `do_something` would be implemented by `WhirledPeas::Command::DoSomething`, which needs to inherit from this class.

# File lib/whirled_peas/command/base.rb, line 9
def self.command_name
  name.split('::').last.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
end
description() click to toggle source
# File lib/whirled_peas/command/base.rb, line 13
def self.description
end
new(args, config) click to toggle source
# File lib/whirled_peas/command/base.rb, line 22
def initialize(args, config)
  @args = args
  @config = config
end
print_usage() click to toggle source

Public Instance Methods

build_logger() click to toggle source
# File lib/whirled_peas/command/base.rb, line 35
def build_logger
  require 'logger'

  if config.log_file.is_a?(IO)
    output = config.log_file
  else
    File.open(config.log_file, 'a')
  end

  logger = Logger.new(output)
  logger.level = config.log_level
  logger.formatter = config.log_formatter
  logger
end
command_name() click to toggle source

Returns the name of the command as expected by the command line script. By convention, this name is snake case version of the class name, e.g. the command `do_something` would be implemented by `WhirledPeas::Command::DoSomething`, which needs to inherit from this class.

# File lib/whirled_peas/command/base.rb, line 31
def command_name
  self.class.command_name
end
print_error() click to toggle source

Display the validation error and print a usage statement

start() click to toggle source

Commands that inherit from this class must override this method

# File lib/whirled_peas/command/base.rb, line 64
def start
end
valid?() click to toggle source

@return [true|false] true if all of the required options were provided

# File lib/whirled_peas/command/base.rb, line 51
def valid?
  @error_text = nil
  validate!
  @error_text.nil?
end

Private Instance Methods

options_usage() click to toggle source
# File lib/whirled_peas/command/base.rb, line 81
def options_usage
end
print_usage() click to toggle source
validate!() click to toggle source

Commands that inherit from this class can override this method to validate command line options

# File lib/whirled_peas/command/base.rb, line 77
def validate!
  # Set @error_text if the options are not valid
end