Class: Arcanus::Command::Base Abstract

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/arcanus/command/base.rb

Overview

This class is abstract.

Abstract base class of all commands.

Direct Known Subclasses

Edit, Export, Help, Setup, Show, Unlock, Version

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

camel_case, deep_dup, snake_case

Constructor Details

#initialize(config, ui, arguments) ⇒ Base

Returns a new instance of Base.

Parameters:



42
43
44
45
46
# File 'lib/arcanus/command/base.rb', line 42

def initialize(config, ui, arguments)
  @config = config
  @ui = ui
  @arguments = arguments
end

Class Method Details

.description(desc = nil) ⇒ Object



29
30
31
32
# File 'lib/arcanus/command/base.rb', line 29

def description(desc = nil)
  @description = desc if desc
  @description
end

.from_arguments(config, ui, arguments) ⇒ Arcanus::Command::Base

Create a command from a list of arguments.

Parameters:

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/arcanus/command/base.rb', line 16

def from_arguments(config, ui, arguments)
  cmd = arguments.first

  begin
    require "arcanus/command/#{Arcanus::Utils.snake_case(cmd)}"
  rescue LoadError
    raise Arcanus::Errors::CommandInvalidError,
          "`arcanus #{cmd}` is not a valid command"
  end

  Arcanus::Command.const_get(Arcanus::Utils.camel_case(cmd)).new(config, ui, arguments)
end

.short_nameObject



34
35
36
# File 'lib/arcanus/command/base.rb', line 34

def short_name
  name.split('::').last.downcase
end

Instance Method Details

#executeObject

Executes the command given the previously-parsed arguments.

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/arcanus/command/base.rb', line 56

def execute
  raise NotImplementedError, 'Define `execute` in Command subclass'
end

#execute_command(command_arguments) ⇒ Object

Executes another command from the same context as this command.

Parameters:

  • command_arguments (Array<String>)


63
64
65
# File 'lib/arcanus/command/base.rb', line 63

def execute_command(command_arguments)
  self.class.from_arguments(config, ui, command_arguments).execute
end

#runObject

Parses arguments and executes the command.



49
50
51
52
53
# File 'lib/arcanus/command/base.rb', line 49

def run
  # TODO: include a parse step here and remove duplicate parsing code from
  # individual commands
  execute
end