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.
359 360 361 362 363 364 |
# File 'lib/ggem/cli/commands.rb', line 359 def initialize(&unknown_cmd_block) @lookup = Hash.new{ |_h, k| unknown_cmd_block.call(k) } @names = [] @aliases = {} @summaries = {} end |
Instance Method Details
#[](name) ⇒ Object
387 388 389 |
# File 'lib/ggem/cli/commands.rb', line 387 def [](name) @lookup[name] end |
#add(klass, name, *aliases) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/ggem/cli/commands.rb', line 366 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
380 381 382 383 384 385 |
# File 'lib/ggem/cli/commands.rb', line 380 def remove(name) @lookup.delete(name) @names.delete(name) @aliases.delete(name) @to_s = nil end |
#size ⇒ Object
391 392 393 |
# File 'lib/ggem/cli/commands.rb', line 391 def size @names.size end |
#to_s ⇒ Object
395 396 397 398 399 400 401 402 403 |
# File 'lib/ggem/cli/commands.rb', line 395 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 |