Class: CommandKit::Commands::AutoRequire
- Inherits:
-
Module
- Object
- Module
- CommandKit::Commands::AutoRequire
- 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
-
#dir ⇒ String
readonly
private
The directory to attempt to require command files within.
-
#namespace ⇒ String
readonly
private
The namespace to lookup command classes within.
Instance Method Summary collapse
-
#command(command_name) ⇒ Class?
private
Attempts to load the command from the #dir and #namespace.
-
#const_get(constant) ⇒ Class
private
Resolves the constant for the command class within the #namespace.
-
#included(command) ⇒ Object
private
Includes CommandKit::Commands and adds a default proc to .commands.
-
#initialize(dir:, namespace:) ⇒ AutoRequire
constructor
Initializes.
-
#join(name) ⇒ String
private
Returns the path for the given command name.
-
#require(file_name) ⇒ Boolean
private
Requires a file within the #dir.
Constructor Details
#initialize(dir:, namespace:) ⇒ AutoRequire
Initializes.
52 53 54 55 |
# File 'lib/command_kit/commands/auto_require.rb', line 52 def initialize(dir: , namespace: ) @dir = dir @namespace = namespace end |
Instance Attribute Details
#dir ⇒ String (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.
32 33 34 |
# File 'lib/command_kit/commands/auto_require.rb', line 32 def dir @dir end |
#namespace ⇒ String (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.
39 40 41 |
# File 'lib/command_kit/commands/auto_require.rb', line 39 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.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# 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) ⇒ 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.
101 102 103 |
# File 'lib/command_kit/commands/auto_require.rb', line 101 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.
144 145 146 147 148 149 150 151 |
# 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) ⇒ 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.
68 69 70 |
# File 'lib/command_kit/commands/auto_require.rb', line 68 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.
83 84 85 |
# File 'lib/command_kit/commands/auto_require.rb', line 83 def require(file_name) super(join(file_name)) end |