Class: CommandKit::Commands::AutoLoad::Subcommand

Inherits:
Subcommand
  • Object
show all
Defined in:
lib/command_kit/commands/auto_load/subcommand.rb

Overview

Represents a registered subcommand that will be auto-loaded.

Instance Attribute Summary collapse

Attributes inherited from Subcommand

#aliases

Instance Method Summary collapse

Methods inherited from Subcommand

summary

Constructor Details

#initialize(constant, path, summary: nil, **kwargs) ⇒ Subcommand

Initializes the lazy-loaded subcommand.

Parameters:

  • path (String)
  • constant (String)
  • summary (String, nil) (defaults to: nil)

    A short summary for the subcommand.

  • kwargs (Hash{Symbol => Object})

    Keyword arguments.

Options Hash (**kwargs):

  • aliases (Array<String>)

    Optional alias names for the subcommand.



44
45
46
47
48
49
# File 'lib/command_kit/commands/auto_load/subcommand.rb', line 44

def initialize(constant, path, summary: nil, **kwargs)
  @constant = constant
  @path     = path

  super(nil, summary: summary, **kwargs)
end

Instance Attribute Details

#constantString (readonly)

The fully qualified constant of the command class.

Returns:

  • (String)


16
17
18
# File 'lib/command_kit/commands/auto_load/subcommand.rb', line 16

def constant
  @constant
end

#pathString (readonly)

The path to the file containing the command class.

Returns:

  • (String)


21
22
23
# File 'lib/command_kit/commands/auto_load/subcommand.rb', line 21

def path
  @path
end

#summaryString? (readonly)

A short summary for the sub-command.

Returns:

  • (String, nil)


26
27
28
# File 'lib/command_kit/commands/auto_load/subcommand.rb', line 26

def summary
  @summary
end

Instance Method Details

#commandClass

Lazy-loads the command class.

Returns:

  • (Class)

    The command class.

Raises:

  • (LoadError)

    Could not load the given #path.

  • (NameError)

    Could not resolve the #constant for the command class.



85
86
87
88
89
90
# File 'lib/command_kit/commands/auto_load/subcommand.rb', line 85

def command
  @command ||= (
    require!
    const_get
  )
end

#const_getClass

Resolves the #constant for the command class.

Returns:

  • (Class)

    The command class.

Raises:

  • (NameError)

    The command class could not be found.



69
70
71
# File 'lib/command_kit/commands/auto_load/subcommand.rb', line 69

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

#require!Boolean

Requires the file.

Returns:

  • (Boolean)


56
57
58
# File 'lib/command_kit/commands/auto_load/subcommand.rb', line 56

def require!
  require(@path)
end