class Brainstem::Cli

Constants

EXECUTABLE_NAME

Attributes

_args[RW]

Holds a copy of the initial given args for debugging purposes.

command_method[W]

Stores the method we should call to run the user command.

log_method[W]

Stores the method we should use to log messages.

requested_command[RW]

Storage for the extracted command.

Public Class Methods

call(args, options = {}) click to toggle source

Convenience for instantiating and calling the Cli object.

@return [Brainstem::Cli] the created instance

# File lib/brainstem/cli.rb, line 19
def self.call(args, options = {})
  new(args, options).call
end
new(args, options = {}) click to toggle source

Creates a new instance of the Cli to respond to user input.

Input is expected to be the name of the subcommand, followed by any additional arguments.

Calls superclass method Brainstem::Concerns::Optional::new
# File lib/brainstem/cli.rb, line 29
def initialize(args, options = {})
  super options

  self._args              = args.dup.freeze
  self.requested_command  = args.shift
end

Public Instance Methods

call() click to toggle source

Routes to an application endpoint depending on given options.

@return [Brainstem::Cli] the instance

# File lib/brainstem/cli.rb, line 41
def call
  if requested_command && commands.has_key?(requested_command)
    self.command_method = commands[requested_command].method(:call)
  end

  command_method.call(_args.drop(1))

  self
end

Private Instance Methods

command_method() click to toggle source

Reader for the method to invoke. By default, will output the help text when called.

@return [Proc] An object responding to #call that is used as the main point execution.

# File lib/brainstem/cli.rb, line 107
def command_method
  @command_method ||= Proc.new do
    # By default, serve help.
    log_method.call(help_text)
  end
end
commands() click to toggle source

A basic routing table where the keys are the command to invoke, and where the value is a callable object or class that will be called with the command_options.

@return [Hash] A hash of +'command' => Callable+

# File lib/brainstem/cli.rb, line 83
def commands
  { 'generate' => Brainstem::CLI::GenerateApiDocsCommand }
end
help_text() click to toggle source

Retrieves the help text and subs any placeholder values.

# File lib/brainstem/cli.rb, line 90
def help_text
  @help_text ||= File.read(File.expand_path('../help_text.txt', __FILE__))
    .gsub('EXECUTABLE_NAME', EXECUTABLE_NAME)
end
log_method() click to toggle source

Reader for the method to log. By default, will print to stdout when called.

We return a proc here because it's much tricker to make assertions against stdout than it is to allow ourselves to inject a proc.

@return [Proc] An object responding to #call that is used to print information.

# File lib/brainstem/cli.rb, line 129
def log_method
  @log_method ||= $stdout.method(:puts)
end
valid_options() click to toggle source

A whitelist of valid options that can be applied to the instance.

@returns [Array<String>]

# File lib/brainstem/cli.rb, line 70
def valid_options
  super | [
    :log_method
  ]
end