Class: TransformedShellCommand
- Defined in:
- lib/transformed_shell_command.rb
Overview
TransformedShellCommand executes a shell command and provides access to a transformed version of the output.
The class accepts:
-
The same arguments as ExecutedShellCommand (command, chdir, env)
-
A regex pattern with named capture groups (supports multi-line output)
-
A format that can be either:
-
A Symbol: calls that method on the output (e.g., :strip, :upcase)
-
A String: format string with named placeholders like ‘%group_name’
-
The command is executed automatically during initialization, and the entire output (including multi-line output) is transformed in a single transformation operation using the format.
The regex pattern can match across multiple lines. Use the multiline flag (m) or construct patterns that handle newlines appropriately.
Basic usage with format string:
regex = /(?<name>\w+):(?<value>\d+)/
format_str = "Name: %{name}, Value: %{value}"
cmd = TransformedShellCommand.new("echo 'user:123'", regex: regex, format: format_str)
cmd.transformed_output # => "Name: user, Value: 123"
cmd.result # => original ExecutedShellCommand::Result
Basic usage with Symbol:
cmd = TransformedShellCommand.new("echo ' hello '", regex: /.*/, format: :strip)
cmd.transformed_output # => "hello"
Multi-line output example:
regex = /(?<first>\w+)\n(?<second>\w+)\n(?<third>\w+)/m
format_str = "%{first}-%{second}-%{third}"
cmd = TransformedShellCommand.new("echo -e 'one\ntwo\nthree'", regex: regex, format: format_str)
cmd.transformed_output # => "one-two-three"
Instance Attribute Summary collapse
-
#chdir ⇒ Object
readonly
Returns the value of attribute chdir.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#regex ⇒ Object
readonly
Returns the value of attribute regex.
-
#transformed_output ⇒ Object
readonly
Returns the transformed output string.
Instance Method Summary collapse
- #duration ⇒ Object
- #exit_code ⇒ Object
- #failure? ⇒ Boolean
- #finished_at ⇒ Object
-
#initialize(command, regex:, format:, chdir: nil, env: {}) ⇒ TransformedShellCommand
constructor
A new instance of TransformedShellCommand.
- #pid ⇒ Object
-
#result ⇒ Object
Returns the original ExecutedShellCommand result.
- #started_at ⇒ Object
- #stderr ⇒ Object
-
#stdout ⇒ Object
Convenience delegators to the original result:.
- #success? ⇒ Boolean
Constructor Details
#initialize(command, regex:, format:, chdir: nil, env: {}) ⇒ TransformedShellCommand
Returns a new instance of TransformedShellCommand.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/transformed_shell_command.rb', line 49 def initialize(command, regex:, format:, chdir: nil, env: {}) @command = command @chdir = chdir @env = env @regex = regex && (regex.is_a?(Regexp) ? regex : Regexp.new(regex)) @format = format @result = nil @transformed_output = nil execute_and_transform end |
Instance Attribute Details
#chdir ⇒ Object (readonly)
Returns the value of attribute chdir.
47 48 49 |
# File 'lib/transformed_shell_command.rb', line 47 def chdir @chdir end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
47 48 49 |
# File 'lib/transformed_shell_command.rb', line 47 def command @command end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
47 48 49 |
# File 'lib/transformed_shell_command.rb', line 47 def env @env end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
47 48 49 |
# File 'lib/transformed_shell_command.rb', line 47 def format @format end |
#regex ⇒ Object (readonly)
Returns the value of attribute regex.
47 48 49 |
# File 'lib/transformed_shell_command.rb', line 47 def regex @regex end |
#transformed_output ⇒ Object (readonly)
Returns the transformed output string. The transformation is performed once during initialization and memoized.
64 65 66 |
# File 'lib/transformed_shell_command.rb', line 64 def transformed_output @transformed_output end |
Instance Method Details
#duration ⇒ Object
95 96 97 |
# File 'lib/transformed_shell_command.rb', line 95 def duration result.duration end |
#exit_code ⇒ Object
83 84 85 |
# File 'lib/transformed_shell_command.rb', line 83 def exit_code result.exit_code end |
#failure? ⇒ Boolean
91 92 93 |
# File 'lib/transformed_shell_command.rb', line 91 def failure? !result.success? end |
#finished_at ⇒ Object
103 104 105 |
# File 'lib/transformed_shell_command.rb', line 103 def finished_at result.finished_at end |
#pid ⇒ Object
107 108 109 |
# File 'lib/transformed_shell_command.rb', line 107 def pid result.pid end |
#result ⇒ Object
Returns the original ExecutedShellCommand result.
69 70 71 |
# File 'lib/transformed_shell_command.rb', line 69 def result @executed_command.result end |
#started_at ⇒ Object
99 100 101 |
# File 'lib/transformed_shell_command.rb', line 99 def started_at result.started_at end |
#stderr ⇒ Object
79 80 81 |
# File 'lib/transformed_shell_command.rb', line 79 def stderr result.stderr end |
#stdout ⇒ Object
Convenience delegators to the original result:
75 76 77 |
# File 'lib/transformed_shell_command.rb', line 75 def stdout result.stdout end |
#success? ⇒ Boolean
87 88 89 |
# File 'lib/transformed_shell_command.rb', line 87 def success? result.success? end |