Class: Amp::Command::Base

Inherits:
Object show all
Includes:
Validations
Defined in:
lib/amp-front/dispatch/commands/base.rb

Overview

The base class frmo which all comamnds inherit.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations

included, #invalid?, #run_after, #valid?

Class Attribute Details

.desc(*args) ⇒ Object

Returns the value of attribute desc.



79
80
81
# File 'lib/amp-front/dispatch/commands/base.rb', line 79

def desc
  @desc
end

.nameObject

Returns the value of attribute name.



79
80
81
# File 'lib/amp-front/dispatch/commands/base.rb', line 79

def name
  @name
end

.optionsObject

Returns the value of attribute options.



79
80
81
# File 'lib/amp-front/dispatch/commands/base.rb', line 79

def options
  @options
end

Instance Attribute Details

#argumentsObject

These are the runtime options selected when the command is called.



84
85
86
# File 'lib/amp-front/dispatch/commands/base.rb', line 84

def arguments
  @arguments
end

#optionsObject

These are the runtime options selected when the command is called.



84
85
86
# File 'lib/amp-front/dispatch/commands/base.rb', line 84

def options
  @options
end

Class Method Details

.all_commandsObject

This tracks all subclasses (and subclasses of subclasses, etc). Plus, this method is inherited, so Wool::Plugins::Git.all_subclasses will have all subclasses of Wool::Plugins::Git!



89
90
91
# File 'lib/amp-front/dispatch/commands/base.rb', line 89

def self.all_commands
  @all_commands ||= [self]
end

.inherited(klass) ⇒ Object

When a Plugin subclass is subclassed, store the subclass and inform the next superclass up the inheritance hierarchy.



95
96
97
98
99
100
101
102
103
# File 'lib/amp-front/dispatch/commands/base.rb', line 95

def self.inherited(klass)
  klass.name ||= 'anonymous ' + self.name
  self.all_commands << klass
  next_klass = self.superclass
  while next_klass != Amp::Command::Base.superclass
    next_klass.send(:inherited, klass)
    next_klass = next_klass.superclass
  end
end

.on_call(&block) ⇒ Object

Specifies the block to run, or returns the block.



106
107
108
109
110
# File 'lib/amp-front/dispatch/commands/base.rb', line 106

def self.on_call(&block)
  if block_given?
    define_method :call_without_args, &block
  end
end

.opt(*args) ⇒ Object



121
122
123
# File 'lib/amp-front/dispatch/commands/base.rb', line 121

def self.opt(*args)
  self.options << args
end

Instance Method Details

#call(opts, args) ⇒ Object

Runs the command with the provided options and arguments.



126
127
128
129
130
# File 'lib/amp-front/dispatch/commands/base.rb', line 126

def call(opts, args)
  self.options = opts
  self.arguments = args
  call_without_args
end

#collect_options(input_args) ⇒ Object

Collects the options specific to this command and returns them.



133
134
135
136
137
138
139
140
141
142
# File 'lib/amp-front/dispatch/commands/base.rb', line 133

def collect_options(input_args)
  args = input_args.dup
  base_options = self.class.options  # Trollop::options uses instance_eval
  @parser, hash = Trollop::options(args) do
    base_options.each do |option|
      opt *option
    end
  end
  [hash, args]
end

#educationObject



144
145
146
147
148
149
150
151
152
# File 'lib/amp-front/dispatch/commands/base.rb', line 144

def education
  if @parser
    output = StringIO.new
    @parser.educate(output)
    output.string
  else
    ''
  end
end