Class: CommandKit::Commands::AutoLoad

Inherits:
Module
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(dir:, namespace:) {|self| ... } ⇒ AutoLoad

Initializes the namespace.

Parameters:

  • dir (String)

    The path to the directory containing the command files.

  • namespace (Module, Class, String)

    The namespace constant that contains the command classes.

Yields:

  • (self)

    If a block is given, it will be used to explicitly map the files within #dir as commands.



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

#commandsHash{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.

Returns:



45
46
47
# File 'lib/command_kit/commands/auto_load.rb', line 45

def commands
  @commands
end

#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 path to the directory containing the command files.

Returns:

  • (String)


52
53
54
# File 'lib/command_kit/commands/auto_load.rb', line 52

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 that the will contain the command classes.

Returns:

  • (String)


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.

Parameters:

  • name (#to_s)

    The name of the command.

  • constant (String)

    The constant name of the command class.

  • file (String)

    The file name of the command class.

  • kwargs (Hash{Symbol => Object})

    Keyword arguments.

Options Hash (**kwargs):

  • summary (String, nil)

    An optional summary for the command.

  • aliases (Array<String>)

    Optional alias names for the subcommand.



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

#filesArray<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.

Returns:

  • (Array<String>)

    The paths to the .rb files in the 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.

Parameters:



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.

Parameters:

  • path (String)

    The relative path.

Returns:

  • (String)

    The joined absolute path.



138
139
140
# File 'lib/command_kit/commands/auto_load.rb', line 138

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