Class: Cartage::Command

Inherits:
CmdParse::Command
  • Object
show all
Defined in:
lib/cartage/command.rb

Overview

The base Cartage command object. This is used to provide commands with a reference to the Cartage instance (@cartage) that is running under the main command-line program.

class Cartage::InfoCommand < Cartage::Command
  def initialize(cartage)
    super(cartage, 'info')
  end

  def perform
    puts 'info'
  end
end

Cartage::Command changes the class-based command protocol from CmdParse so that you must define #perform instead of #execute. This is so that certain common behaviours (such as common config resolution) can be performed for all commands.

Instance Method Summary collapse

Constructor Details

#initialize(cartage, name, takes_commands: false) ⇒ Command

Create a ::CmdParse::Command with the given name. Save a reference to the provided cartage object.


26
27
28
29
30
31
32
33
34
# File 'lib/cartage/command.rb', line 26

def initialize(cartage, name, takes_commands: false)
  if self.instance_of?(Cartage::Command)
    raise ArgumentError, 'Cartage::Command cannot be instantiated.'
  end

  super(name, takes_commands: takes_commands)

  @cartage = cartage
end

Instance Method Details

#execute(*args) ⇒ Object

Run the command. Children must not implement this method.


37
38
39
40
# File 'lib/cartage/command.rb', line 37

def execute(*args)
  @cartage.send(:resolve_config!, *Array(with_plugins))
  perform(*args)
end

#with_pluginsObject

This optional method is implemented by a plug-in to instruct Cartage to resolve the plug-in configuration for the specified plug-ins. Specify the plug-ins to be resolved as an array of Symbols.

Plug-ins resolve by overriding the private method Cartage::Plugin#resolve_config!.


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

def with_plugins
  []
end