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.
322 323 324 325 326 327 |
# File 'lib/ardb/cli/commands.rb', line 322 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
349 350 351 |
# File 'lib/ardb/cli/commands.rb', line 349 def [](cmd_name) @lookup[cmd_name] end |
#add(klass) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/ardb/cli/commands.rb', line 329 def add(klass) begin cmd = klass.new rescue StandardError # 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
342 343 344 345 346 347 |
# File 'lib/ardb/cli/commands.rb', line 342 def remove(klass) @lookup.delete(klass.command_name) @names.delete(klass.command_name) @summaries.delete(klass.command_name) @to_s = nil end |
#size ⇒ Object
353 354 355 |
# File 'lib/ardb/cli/commands.rb', line 353 def size @names.size end |
#to_s ⇒ Object
357 358 359 360 361 362 363 |
# File 'lib/ardb/cli/commands.rb', line 357 def to_s max_name_size = @names.map{ |n| n.size }.max || 0 @to_s ||= @names.map do |n| "#{n.ljust(max_name_size)} #{@summaries[n]}" end.join("\n") end |