class CommandKit::Commands::AutoRequire

Adds a catch-all that attempts to load missing commands from a directory/namespace.

## Examples

module Foo
  class CLI

    include CommandKit::Commands
    include CommandKit::Commands::AutoRequire.new(
      dir:       'foo/bar/commands',
      namespace: 'Foo::CLI::Commands'
    )

  end
end

Attributes

dir[R]

The directory to attempt to require command files within.

@return [String]

@api private

namespace[R]

The namespace to lookup command classes within.

@return [String]

@api private

Public Class Methods

new(dir: , namespace: ) click to toggle source

Initializes.

@param [String] dir

The directory to require commands from.

@param [String] namespace

The namespace to search for command classes in.

@api public

# File lib/command_kit/commands/auto_require.rb, line 52
def initialize(dir: , namespace: )
  @dir       = dir
  @namespace = namespace
end

Public Instance Methods

command(command_name) click to toggle source

Attempts to load the command from the {#dir} and {#namespace}.

@param [String] command_name

The given command name.

@return [Class, nil]

The command's class, or `nil` if the command cannot be loaded from
{#dir} or found within {#namespace}.

@api private

# File lib/command_kit/commands/auto_require.rb, line 117
def command(command_name)
  file_name = Inflector.underscore(command_name)

  begin
    require(file_name)
  rescue LoadError
    return
  end

  constant = Inflector.camelize(file_name)

  begin
    const_get(constant)
  rescue NameError
    return
  end
end
const_get(constant) click to toggle source

Resolves the constant for the command class within the {#namespace}.

@param [String] constant

The constant name.

@return [Class]

The command class.

@raise [NameError]

The command class could not be found within the {#namespace}.

@api private

# File lib/command_kit/commands/auto_require.rb, line 101
def const_get(constant)
  Object.const_get("::#{@namespace}::#{constant}",false)
end
included(command) click to toggle source

Includes {Commands} and adds a default proc to {Commands::ClassMethods#commands .commands}.

@param [Class] command

The command class including {AutoRequire}.

@api private

# File lib/command_kit/commands/auto_require.rb, line 144
def included(command)
  command.include Commands
  command.commands.default_proc = ->(hash,key) {
    hash[key] = if (command_class = command(key))
                  Commands::Subcommand.new(command_class)
                end
  }
end
join(name) click to toggle source

Returns the path for the given command name.

@param [String] name

The given command name.

@return [String]

The path to the file that should contain the command.

@api private

# File lib/command_kit/commands/auto_require.rb, line 68
def join(name)
  File.join(@dir,name)
end
require(file_name) click to toggle source

Requires a file within the {#dir}.

@param [String] file_name

@return [Boolean]

@raise [LoadError]

@api private

Calls superclass method
# File lib/command_kit/commands/auto_require.rb, line 83
def require(file_name)
  super(join(file_name))
end