Class: Etna::Command

Inherits:
Object
  • Object
show all
Includes:
CommandOrExecutor
Defined in:
lib/etna/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CommandOrExecutor

#command_name, #completions_for, #desc, #flag_argspec, #flag_as_parameter, included, #parse_flags, #program_name, #usage

Constructor Details

#initialize(parent = nil) ⇒ Command

Returns a new instance of Command.



224
225
226
# File 'lib/etna/command.rb', line 224

def initialize(parent = nil)
  @parent = parent
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



222
223
224
# File 'lib/etna/command.rb', line 222

def parent
  @parent
end

Class Method Details

.parent_scopeObject



228
229
230
231
232
# File 'lib/etna/command.rb', line 228

def self.parent_scope
  parts = self.name.split('::')
  parts.pop
  Kernel.const_get(parts.join('::'))
end

Instance Method Details

#completionsObject



247
248
249
250
251
252
253
254
255
256
# File 'lib/etna/command.rb', line 247

def completions
  method(:execute).parameters.map do |type, name|
    name = "..." if name.nil?
    if type == :req || type == :opt
      [completions_for(name)]
    else
      []
    end
  end.inject([], &:+)
end

#executeObject

To be overridden during inheritance.



259
260
261
262
263
264
# File 'lib/etna/command.rb', line 259

def execute
  raise 'Command is not implemented'
rescue => e
  Rollbar.error(e)
  raise
end

#fill_in_missing_params(args) ⇒ Object



239
240
241
242
243
244
245
# File 'lib/etna/command.rb', line 239

def fill_in_missing_params(args)
  req_params = method(:execute).parameters.select { |type, name| type == :req }
  args + (req_params[(args.length)..(req_params.length)] || []).map do |type, name|
    puts "#{name}?"
    STDIN.gets.chomp
  end
end

#find_command(*args, **kwds) ⇒ Object



234
235
236
237
# File 'lib/etna/command.rb', line 234

def find_command(*args, **kwds)
  flags, args = parse_flags(*args)
  [self, args, kwds.update(flags)]
end

#setup(config) ⇒ Object

To be overridden during inheritance, to e.g. connect to a database. Should be called with super by inheriting method.



268
269
270
271
272
273
# File 'lib/etna/command.rb', line 268

def setup(config)
  Etna::Application.find(self.class).configure(config)
rescue => e
  Rollbar.error(e)
  raise
end