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

Diff, 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(ui, arguments) ⇒ Base

Returns a new instance of Base.

Parameters:


40
41
42
43
# File 'lib/arcanus/command/base.rb', line 40

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

Class Method Details

.description(desc = nil) ⇒ Object


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

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

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

Create a command from a list of arguments.

Parameters:

Returns:


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

def from_arguments(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(ui, arguments)
end

.short_nameObject


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

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

Instance Method Details

#executeObject

Executes the command given the previously-parsed arguments.

Raises:

  • (NotImplementedError)

53
54
55
# File 'lib/arcanus/command/base.rb', line 53

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>)

60
61
62
# File 'lib/arcanus/command/base.rb', line 60

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

#runObject

Parses arguments and executes the command.


46
47
48
49
50
# File 'lib/arcanus/command/base.rb', line 46

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