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.
384 385 386 387 388 389 |
# File 'lib/ggem/cli/commands.rb', line 384 def initialize(&unknown_cmd_block) @lookup = Hash.new{ |h,k| unknown_cmd_block.call(k) } @names = [] @aliases = {} @summaries = {} end |
Instance Method Details
#[](name) ⇒ Object
412 413 414 |
# File 'lib/ggem/cli/commands.rb', line 412 def [](name) @lookup[name] end |
#add(klass, name, *aliases) ⇒ Object
391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/ggem/cli/commands.rb', line 391 def add(klass, name, *aliases) begin cmd = klass.new rescue StandardError => err # 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
405 406 407 408 409 410 |
# File 'lib/ggem/cli/commands.rb', line 405 def remove(name) @lookup.delete(name) @names.delete(name) @aliases.delete(name) @to_s = nil end |
#size ⇒ Object
416 417 418 |
# File 'lib/ggem/cli/commands.rb', line 416 def size @names.size end |
#to_s ⇒ Object
420 421 422 423 424 425 426 427 |
# File 'lib/ggem/cli/commands.rb', line 420 def to_s max_name_size = @names.map{ |n| n.size }.max || 0 max_alias_size = @aliases.values.map{ |v| v.size }.max || 0 @to_s ||= @names.map do |n| "#{n.ljust(max_name_size)} #{@aliases[n].ljust(max_alias_size)} #{@summaries[n]}" end.join("\n") end |