Class: Amp::Help::HelpEntry

Inherits:
Object show all
Defined in:
lib/amp-front/help/help.rb

Overview

The generic HelpEntry class encapsulates a entry in the help system. The entry has text that it provides to the user, as well as a name. The base HelpEntry class does not track its own name, because well, that’s not useful! All it needs to know how to do is present its text when asked for it.

Direct Known Subclasses

CommandHelpEntry, ErbHelpEntry, MarkdownHelpEntry

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, text = "") ⇒ HelpEntry

Creates a new HelpEntry, and registers it in the Help system, making it immediately available. It is for this reason that all subclasses should call super, because that registration is important!

Parameters:

  • name (String, #to_s)

    the name under which to register this help entry

  • text (String) (defaults to: "")

    (“”) the text for the entry.



121
122
123
124
# File 'lib/amp-front/help/help.rb', line 121

def initialize(name, text = "")
  @text = text
  HelpRegistry.register(name, self)
end

Class Method Details

.from_file(filename) ⇒ HelpEntry

Singleton method that opens a file and returns a HelpEntry representing it. What makes this method spiffy is that it tries to detect the type of file – markdown, ERb, et cetera, based on the file’s extension, and picks the appropriate class to represent that help entry.

The entry is registered under the name of the file - without any extensions - and the file’s full contents are provided as the initial text.

Parameters:

  • filename (String)

    the path to the file to load

Returns:

  • (HelpEntry)

    a help entry representing the file as best as we can.



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/amp-front/help/help.rb', line 100

def from_file(filename)
  klass = case File.extname(filename).downcase
          when ".md", ".markdown"
            MarkdownHelpEntry
          when ".erb"
            ErbHelpEntry
          else
            HelpEntry
          end
  name = File.basename(filename).split(".", 2).first
  klass.new(name, File.read(filename))
end

Instance Method Details

#descObject

Describes the entry briefly, so if the user must pick, they have a decent shot at knowing what this entry is about. Hopefully.

In the generic case, use the text and grab the first few words.

Returns:

  • a description of the entry based on its content



147
148
149
# File 'lib/amp-front/help/help.rb', line 147

def desc
  %Q{a regular help entry ("#{text.split[0..5].join(" ")} ...")}
end

#text(options = {}) ⇒ String

Returns the help text to display for this entry.

In the generic case, just return the @text variable.

Parameters:

  • options (Hash) (defaults to: {})

    the options for the process - that way the help commands can access the user’s run-time options and global configuration. For example, if the user passes in –verbose or –quiet, each help entry could handle that differently. Who are we to judge?

Returns:

  • (String)

    the help text for the entry.



136
137
138
# File 'lib/amp-front/help/help.rb', line 136

def text(options = {})
  @text
end