Class: Ardb::CLI::CommandSet

Inherits:
Object
  • Object
show all
Defined in:
lib/ardb/cli/commands.rb

Instance Method Summary collapse

Constructor Details

#initialize(&unknown_cmd_block) ⇒ CommandSet

Returns a new instance of CommandSet.



411
412
413
414
415
416
# File 'lib/ardb/cli/commands.rb', line 411

def initialize(&unknown_cmd_block)
  @lookup    = Hash.new{ |_h, k| unknown_cmd_block.call(k) }
  @names     = []
  @aliases   = {}
  @summaries = {}
end

Instance Method Details

#[](cmd_name) ⇒ Object



438
439
440
# File 'lib/ardb/cli/commands.rb', line 438

def [](cmd_name)
  @lookup[cmd_name]
end

#add(klass) ⇒ Object



418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/ardb/cli/commands.rb', line 418

def add(klass)
  begin
    cmd = klass.new
  rescue
    # don"t add any commands you can"t initialize
  else
    @lookup[cmd.command_name] = cmd
    @to_s = nil
    @names << cmd.command_name
    @summaries[cmd.command_name] = cmd.command_summary.to_s
  end
end

#remove(klass) ⇒ Object



431
432
433
434
435
436
# File 'lib/ardb/cli/commands.rb', line 431

def remove(klass)
  @lookup.delete(klass.command_name)
  @names.delete(klass.command_name)
  @summaries.delete(klass.command_name)
  @to_s = nil
end

#sizeObject



442
443
444
# File 'lib/ardb/cli/commands.rb', line 442

def size
  @names.size
end

#to_sObject



446
447
448
449
450
451
452
# File 'lib/ardb/cli/commands.rb', line 446

def to_s
  max_name_size = @names.map(&:size).max || 0

  @to_s ||= @names.map{ |n|
    "#{n.ljust(max_name_size)} #{@summaries[n]}"
  }.join("\n")
end