Class: Mercenary::Presenter

Inherits:
Object
  • Object
show all
Defined in:
lib/mercenary/presenter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Presenter

Public: Make a new Presenter

command - a Mercenary::Command to present

Returns nothing



10
11
12
# File 'lib/mercenary/presenter.rb', line 10

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



71
72
73
74
75
76
77
78
# File 'lib/mercenary/presenter.rb', line 71

def method_missing(meth, *args, &block)
  if meth.to_s =~ /^print_(.+)$/
    send("#{$1.downcase}_presentation")
  else
    super # You *must* call super if you don't handle the method,
          # otherwise you'll mess up Ruby's method lookup.
  end
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



3
4
5
# File 'lib/mercenary/presenter.rb', line 3

def command
  @command
end

Instance Method Details

#command_headerObject

Public: Builds the command header, including the command identity and description

Returns the command header as a String



40
41
42
43
44
# File 'lib/mercenary/presenter.rb', line 40

def command_header
  header = "#{command.identity}"
  header << " -- #{command.description}" if command.description
  header
end

#command_presentationObject

Public: Builds a string representation of the whole command

Returns the string representation of the whole command



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mercenary/presenter.rb', line 49

def command_presentation
  msg = []
  msg << command_header
  msg << "Usage:"
  msg << usage_presentation

  if opts = options_presentation
    msg << "Options:\n#{opts}"
  end
  if subcommands = subcommands_presentation
    msg << "Subcommands:\n#{subcommands_presentation}"
  end
  msg.join("\n\n")
end

#options_presentationObject

Public: Builds a string representation of the options

Returns the string representation of the options



24
25
26
27
# File 'lib/mercenary/presenter.rb', line 24

def options_presentation
  return nil unless command.options.size > 0
  command.options.map(&:to_s).join("\n")
end

#subcommands_presentationObject

Public: Builds a string representation of the subcommands

Returns the string representation of the subcommands



32
33
34
35
# File 'lib/mercenary/presenter.rb', line 32

def subcommands_presentation
  return nil unless command.commands.size > 0
  command.commands.values.map(&:summarize).join("\n")
end

#usage_presentationObject

Public: Builds a string representation of the command usage

Returns the string representation of the command usage



17
18
19
# File 'lib/mercenary/presenter.rb', line 17

def usage_presentation
  "  #{command.syntax}"
end