Class: CommandKit::Commands::AutoLoad
- Inherits:
-
Module
- Object
- Module
- CommandKit::Commands::AutoLoad
- Defined in:
- lib/command_kit/commands/auto_load.rb,
lib/command_kit/commands/auto_load/subcommand.rb
Overview
Provides lazy-loading access to a directory / module namespace of command classes.
Examples
class CLI
include CommandKit::Commands::AutoLoad.new(
dir: "#{__dir__}/cli/commands",
namespace: 'CLI::Commands'
)
end
Explicit Mapping
class CLI
include CommandKit::Commands::AutoLoad.new(
dir: "#{__dir__}/cli/commands",
namespace: 'CLI::Commands'
) { |autoload|
autoload.command 'foo', 'Foo', 'foo.rb', summary: 'Foo command'
autoload.command 'bar', 'Bar', 'bar.rb', summary: 'Bar command'
}
end
Defined Under Namespace
Classes: Subcommand
Instance Attribute Summary collapse
-
#commands ⇒ Hash{String => Subcommand}
readonly
private
The auto-load subcommands.
-
#dir ⇒ String
readonly
private
The path to the directory containing the command files.
-
#namespace ⇒ String
readonly
private
The namespace that the will contain the command classes.
Instance Method Summary collapse
-
#command(name, constant, file, **kwargs) ⇒ Object
Defines an auto-loaded command mapping.
-
#files ⇒ Array<String>
private
Returns the files within given directory.
-
#included(command) ⇒ Object
private
Includes CommandKit::Commands and registers all files within the namespace as lazy-loaded subcommands.
-
#initialize(dir:, namespace:) {|self| ... } ⇒ AutoLoad
constructor
Initializes the namespace.
-
#join(path) ⇒ String
private
Joins a relative path with #dir.
Constructor Details
#initialize(dir:, namespace:) {|self| ... } ⇒ AutoLoad
Initializes the namespace.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/command_kit/commands/auto_load.rb', line 76 def initialize(dir: , namespace: ) @commands = {} @dir = dir @namespace = namespace if block_given? yield self else files.each do |path| base_name = File.basename(path) file_name = base_name.chomp('.rb') command_name = Inflector.dasherize(file_name) class_name = Inflector.camelize(file_name) command command_name, class_name, base_name end end end |
Instance Attribute Details
#commands ⇒ Hash{String => Subcommand} (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 auto-load subcommands.
45 46 47 |
# File 'lib/command_kit/commands/auto_load.rb', line 45 def commands @commands end |
#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 path to the directory containing the command files.
52 53 54 |
# File 'lib/command_kit/commands/auto_load.rb', line 52 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 that the will contain the command classes.
59 60 61 |
# File 'lib/command_kit/commands/auto_load.rb', line 59 def namespace @namespace end |
Instance Method Details
#command(name, constant, file, **kwargs) ⇒ Object
Defines an auto-loaded command mapping.
119 120 121 122 123 124 125 |
# File 'lib/command_kit/commands/auto_load.rb', line 119 def command(name, constant, file, **kwargs) @commands[name.to_s] = Subcommand.new( "#{@namespace}::#{constant}", join(file), **kwargs ) end |
#files ⇒ Array<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 files within given directory.
150 151 152 |
# File 'lib/command_kit/commands/auto_load.rb', line 150 def files Dir.glob(join('*.rb')) 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 registers all files within the namespace as lazy-loaded subcommands.
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/command_kit/commands/auto_load.rb', line 163 def included(command) command.include Commands @commands.each do |name,subcommand| command.commands[name] = subcommand subcommand.aliases.each do |alias_name| command.command_aliases[alias_name] = name end end end |
#join(path) ⇒ 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.
Joins a relative path with #dir.
138 139 140 |
# File 'lib/command_kit/commands/auto_load.rb', line 138 def join(path) File.join(@dir,path) end |