class Toys::StandardMiddleware::ShowRootVersion

A middleware that displays a version string for the root tool if the `–version` flag is given.

Constants

DEFAULT_VERSION_FLAGS

Default version flags @return [Array<String>]

DEFAULT_VERSION_FLAG_DESC

Default description for the version flags @return [String]

SHOW_VERSION_KEY

Key set when the version flag is present @return [Object]

Public Class Methods

new(version_string: nil, version_flags: DEFAULT_VERSION_FLAGS, version_flag_desc: DEFAULT_VERSION_FLAG_DESC, stream: $stdout) click to toggle source

Create a ShowVersion middleware

@param version_string [String] The string that should be displayed. @param version_flags [Array<String>] A list of flags that should

trigger displaying the version. Default is
{DEFAULT_VERSION_FLAGS}.

@param stream [IO] Output stream to write to. Default is stdout.

# File lib/toys/standard_middleware/show_root_version.rb, line 37
def initialize(version_string: nil,
               version_flags: DEFAULT_VERSION_FLAGS,
               version_flag_desc: DEFAULT_VERSION_FLAG_DESC,
               stream: $stdout)
  @version_string = version_string
  @version_flags = version_flags
  @version_flag_desc = version_flag_desc
  @output = stream
end

Public Instance Methods

config(tool, _loader) { || ... } click to toggle source

Adds the version flag if requested. @private

# File lib/toys/standard_middleware/show_root_version.rb, line 51
def config(tool, _loader)
  if @version_string && tool.root?
    tool.add_flag(SHOW_VERSION_KEY, @version_flags,
                  report_collisions: false, desc: @version_flag_desc)
  end
  yield
end
run(context) { || ... } click to toggle source

This middleware displays the version. @private

# File lib/toys/standard_middleware/show_root_version.rb, line 63
def run(context)
  if context[SHOW_VERSION_KEY]
    @output.puts(@version_string)
  else
    yield
  end
end