Module: Redwing::Command

Defined in:
lib/redwing/command.rb,
lib/redwing/command/base_command.rb

Defined Under Namespace

Classes: BaseCommand

Constant Summary collapse

COMMAND_WHITELIST =
{
  console: %w[console c],
  server: %w[server s],
  new: %w[new]
}.freeze
VERSION_MAPPINGS =
%w[-v --version].to_set

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.argsObject (readonly)

Returns the value of attribute args.



21
22
23
# File 'lib/redwing/command.rb', line 21

def args
  @args
end

.argvObject (readonly)

Returns the value of attribute argv.



21
22
23
# File 'lib/redwing/command.rb', line 21

def argv
  @argv
end

Class Method Details

.command_klassObject



57
58
59
60
61
62
63
# File 'lib/redwing/command.rb', line 57

def command_klass
  @command_klass ||= {
    console: Redwing::Commands::ConsoleCommand,
    new: Redwing::Commands::NewCommand,
    server: Redwing::Commands::ServerCommand
  }[command_name]
end

.command_nameObject



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

def command_name
  @command_name ||= COMMAND_WHITELIST.find { |_, aliases| aliases.include?(@argv.first) }&.first
end

.execute(argv) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/redwing/command.rb', line 23

def execute(argv)
  return puts Redwing::VERSION if VERSION_MAPPINGS.include?(argv.first)

  @argv = argv
  @args = @argv.drop(1)

  raise Error::UnknownCommand, "Unknown command: #{@argv.first}" if command_klass.nil?

  exec_klass = command_klass.new(@args, command_options)
  unless exec_klass.respond_to?(:perform)
    raise Error::PerformNotImplemented, "#{exec_klass.class} does not implement #perform"
  end

  trap_interrupt!
  exec_klass.perform

  exit_ok!
rescue StandardError => e
  shell.say(e.message)

  exit_failed!
end

.reset!Object



46
47
48
49
50
51
# File 'lib/redwing/command.rb', line 46

def reset!
  @argv = nil
  @args = nil
  @command_name = nil
  @command_klass = nil
end