Class: Ardb::CLI::CommandSet
- Inherits:
-
Object
- Object
- Ardb::CLI::CommandSet
- Defined in:
- lib/ardb/cli/commands.rb
Instance Method Summary collapse
- #[](cmd_name) ⇒ Object
- #add(klass) ⇒ Object
-
#initialize(&unknown_cmd_block) ⇒ CommandSet
constructor
A new instance of CommandSet.
- #remove(klass) ⇒ Object
- #size ⇒ Object
- #to_s ⇒ Object
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 |
#size ⇒ Object
442 443 444 |
# File 'lib/ardb/cli/commands.rb', line 442 def size @names.size end |
#to_s ⇒ Object
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 |