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.
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
#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.
34 35 36 |
# File 'lib/command_kit/commands/auto_require.rb', line 34 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.
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.
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.
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.
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.
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.
85 86 87 |
# File 'lib/command_kit/commands/auto_require.rb', line 85 def require(file_name) super(join(file_name)) end |