Class: Toys::StandardMiddleware::ShowRootVersion
- Inherits:
-
Object
- Object
- Toys::StandardMiddleware::ShowRootVersion
- Includes:
- Middleware
- Defined in:
- lib/toys/standard_middleware/show_root_version.rb
Overview
A middleware that displays a version string for the root tool if the
--version flag is given.
Constant Summary collapse
- DEFAULT_VERSION_FLAGS =
Default version flags
["--version"].freeze
- DEFAULT_VERSION_FLAG_DESC =
Default description for the version flags
"Display the version"- SHOW_VERSION_KEY =
Key set when the version flag is present
Object.new.freeze
Instance Method Summary collapse
-
#config(tool_definition, _loader) ⇒ Object
Adds the version flag if requested.
-
#initialize(version_string: nil, version_flags: DEFAULT_VERSION_FLAGS, version_flag_desc: DEFAULT_VERSION_FLAG_DESC, stream: $stdout) ⇒ ShowRootVersion
constructor
Create a ShowVersion middleware.
-
#run(tool) ⇒ Object
This middleware displays the version.
Constructor Details
#initialize(version_string: nil, version_flags: DEFAULT_VERSION_FLAGS, version_flag_desc: DEFAULT_VERSION_FLAG_DESC, stream: $stdout) ⇒ ShowRootVersion
Create a ShowVersion middleware
68 69 70 71 72 73 74 75 76 |
# File 'lib/toys/standard_middleware/show_root_version.rb', line 68 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 @terminal = Utils::Terminal.new(output: stream) end |
Instance Method Details
#config(tool_definition, _loader) ⇒ Object
Adds the version flag if requested.
81 82 83 84 85 86 87 |
# File 'lib/toys/standard_middleware/show_root_version.rb', line 81 def config(tool_definition, _loader) if @version_string && tool_definition.root? tool_definition.add_flag(SHOW_VERSION_KEY, @version_flags, report_collisions: false, desc: @version_flag_desc) end yield end |
#run(tool) ⇒ Object
This middleware displays the version.
92 93 94 95 96 97 98 |
# File 'lib/toys/standard_middleware/show_root_version.rb', line 92 def run(tool) if tool[SHOW_VERSION_KEY] @terminal.puts(@version_string) else yield end end |