Class: GGem::CLI::CommandSet
- Inherits:
-
Object
- Object
- GGem::CLI::CommandSet
- Defined in:
- lib/ggem/cli/commands.rb
Instance Method Summary collapse
- #[](name) ⇒ Object
- #add(klass, name, *aliases) ⇒ Object
-
#initialize(&unknown_cmd_block) ⇒ CommandSet
constructor
A new instance of CommandSet.
- #remove(name) ⇒ Object
- #size ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(&unknown_cmd_block) ⇒ CommandSet
Returns a new instance of CommandSet.
361 362 363 364 365 366 |
# File 'lib/ggem/cli/commands.rb', line 361 def initialize(&unknown_cmd_block) @lookup = Hash.new{ |_h, k| unknown_cmd_block.call(k) } @names = [] @aliases = {} @summaries = {} end |
Instance Method Details
#[](name) ⇒ Object
389 390 391 |
# File 'lib/ggem/cli/commands.rb', line 389 def [](name) @lookup[name] end |
#add(klass, name, *aliases) ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/ggem/cli/commands.rb', line 368 def add(klass, name, *aliases) begin cmd = klass.new rescue # don't add any commands you can't init else ([name] + aliases).each{ |n| @lookup[n] = cmd } @to_s = nil @names << name @aliases[name] = aliases.empty? ? "" : "(#{aliases.join(", ")})" @summaries[name] = cmd.summary.to_s.empty? ? "" : "# #{cmd.summary}" end end |
#remove(name) ⇒ Object
382 383 384 385 386 387 |
# File 'lib/ggem/cli/commands.rb', line 382 def remove(name) @lookup.delete(name) @names.delete(name) @aliases.delete(name) @to_s = nil end |
#size ⇒ Object
393 394 395 |
# File 'lib/ggem/cli/commands.rb', line 393 def size @names.size end |
#to_s ⇒ Object
397 398 399 400 401 402 403 404 405 |
# File 'lib/ggem/cli/commands.rb', line 397 def to_s max_name_size = @names.map(&:size).max || 0 max_alias_size = @aliases.values.map(&:size).max || 0 @to_s ||= @names.map{ |n| "#{n.ljust(max_name_size)} #{@aliases[n].ljust(max_alias_size)} "\ "#{@summaries[n]}" }.join("\n") end |