Class: Mercenary::Presenter
- Inherits:
-
Object
- Object
- Mercenary::Presenter
- Defined in:
- lib/mercenary/presenter.rb
Instance Attribute Summary collapse
-
#command ⇒ Object
Returns the value of attribute command.
Instance Method Summary collapse
-
#command_header ⇒ Object
Public: Builds the command header, including the command identity and description.
- #command_options_presentation ⇒ Object
-
#command_presentation ⇒ Object
Public: Builds a string representation of the whole command.
-
#initialize(command) ⇒ Presenter
constructor
Public: Make a new Presenter.
-
#method_missing(meth, *args, &block) ⇒ Object
Public: Turn a print_* into a *_presentation or freak out.
-
#options_presentation ⇒ Object
Public: Builds a string representation of the options.
-
#parent_command_options_presentation ⇒ Object
Public: Builds a string representation of the options for parent commands.
-
#subcommands_presentation ⇒ Object
Public: Builds a string representation of the subcommands.
-
#usage_presentation ⇒ Object
Public: Builds a string representation of the command usage.
Constructor Details
#initialize(command) ⇒ Presenter
Public: Make a new Presenter
command - a Mercenary::Command to present
Returns nothing
12 13 14 |
# File 'lib/mercenary/presenter.rb', line 12 def initialize(command) @command = command end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
Public: Turn a print_* into a *_presentation or freak out
meth - the method being called args - an array of arguments passed to the missing method block - the block passed to the missing method
Returns the value of whatever function is called
87 88 89 90 91 92 93 94 95 |
# File 'lib/mercenary/presenter.rb', line 87 def method_missing(meth, *args, &block) if meth.to_s =~ %r!^print_(.+)$! send("#{Regexp.last_match(1).downcase}_presentation") else # You *must* call super if you don't handle the method, # otherwise you'll mess up Ruby's method lookup. super end end |
Instance Attribute Details
#command ⇒ Object
Returns the value of attribute command.
5 6 7 |
# File 'lib/mercenary/presenter.rb', line 5 def command @command end |
Instance Method Details
#command_header ⇒ Object
Public: Builds the command header, including the command identity and description
Returns the command header as a String
56 57 58 59 60 |
# File 'lib/mercenary/presenter.rb', line 56 def command_header header = command.identity.to_s header << " -- #{command.description}" if command.description header end |
#command_options_presentation ⇒ Object
31 32 33 34 |
# File 'lib/mercenary/presenter.rb', line 31 def return nil if command..empty? command..map(&:to_s).join("\n") end |
#command_presentation ⇒ Object
Public: Builds a string representation of the whole command
Returns the string representation of the whole command
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mercenary/presenter.rb', line 65 def command_presentation msg = [] msg << command_header msg << "Usage:" msg << usage_presentation if opts = msg << "Options:\n#{opts}" end if subcommands = subcommands_presentation msg << "Subcommands:\n#{subcommands_presentation}" end msg.join("\n\n") end |
#options_presentation ⇒ Object
Public: Builds a string representation of the options
Returns the string representation of the options
26 27 28 29 |
# File 'lib/mercenary/presenter.rb', line 26 def return nil unless || [, ].compact.join("\n") end |
#parent_command_options_presentation ⇒ Object
Public: Builds a string representation of the options for parent commands
Returns the string representation of the options for parent commands
40 41 42 43 |
# File 'lib/mercenary/presenter.rb', line 40 def return nil unless command.parent Presenter.new(command.parent). end |
#subcommands_presentation ⇒ Object
Public: Builds a string representation of the subcommands
Returns the string representation of the subcommands
48 49 50 51 |
# File 'lib/mercenary/presenter.rb', line 48 def subcommands_presentation return nil if command.commands.empty? command.commands.values.uniq.map(&:summarize).join("\n") end |
#usage_presentation ⇒ Object
Public: Builds a string representation of the command usage
Returns the string representation of the command usage
19 20 21 |
# File 'lib/mercenary/presenter.rb', line 19 def usage_presentation " #{command.syntax}" end |