Class: CommandKit::Commands::AutoRequire

Inherits:
Module
  • Object
show all
Defined in:
lib/command_kit/commands/auto_require.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir:, namespace:) ⇒ AutoRequire

Initializes.

Parameters:

  • dir (String)

    The directory to require commands from.

  • namespace (String)

    The namespace to search for command classes in.



54
55
56
57
# File 'lib/command_kit/commands/auto_require.rb', line 54

def initialize(dir: , namespace: )
  @dir       = dir
  @namespace = namespace
end

Instance Attribute Details

#dirString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The directory to attempt to require command files within.

Returns:

  • (String)


34
35
36
# File 'lib/command_kit/commands/auto_require.rb', line 34

def dir
  @dir
end

#namespaceString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The namespace to lookup command classes within.

Returns:

  • (String)


41
42
43
# File 'lib/command_kit/commands/auto_require.rb', line 41

def namespace
  @namespace
end

Instance Method Details

#command(command_name) ⇒ Class?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • command_name (String)

    The given command name.

Returns:

  • (Class, nil)

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/command_kit/commands/auto_require.rb', line 119

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) ⇒ Class

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • constant (String)

    The constant name.

Returns:

  • (Class)

    The command class.

Raises:

  • (NameError)

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



103
104
105
# File 'lib/command_kit/commands/auto_require.rb', line 103

def const_get(constant)
  Object.const_get("::#{@namespace}::#{constant}",false)
end

#included(command) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Includes CommandKit::Commands and adds a default proc to .commands.

Parameters:



146
147
148
149
150
151
152
153
# File 'lib/command_kit/commands/auto_require.rb', line 146

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) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the path for the given command name.

Parameters:

  • name (String)

    The given command name.

Returns:

  • (String)

    The path to the file that should contain the command.



70
71
72
# File 'lib/command_kit/commands/auto_require.rb', line 70

def join(name)
  File.join(@dir,name)
end

#require(file_name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Requires a file within the #dir.

Parameters:

  • file_name (String)

Returns:

  • (Boolean)

Raises:

  • (LoadError)


85
86
87
# File 'lib/command_kit/commands/auto_require.rb', line 85

def require(file_name)
  super(join(file_name))
end