Class: Utils::ProcessJob
- Includes:
- Term::ANSIColor
- Defined in:
- lib/utils/probe_server.rb
Instance Attribute Summary collapse
-
#args ⇒ Array
readonly
The args reader method provides access to the arguments stored in the instance.
-
#id ⇒ Integer
readonly
Returns the unique identifier of the process job.
-
#ok ⇒ String
The ok method returns a character representation of the job’s success status.
Instance Method Summary collapse
-
#as_json ⇒ Hash
The as_json method converts the process job object into a JSON-serializable hash.
-
#initialize(args:, probe_server: nil) ⇒ Utils::ProcessJob
constructor
Initializes a new ProcessJob instance with the specified arguments and optional probe server.
-
#inspect ⇒ String
(also: #to_s)
The inspect method generates a colorized string representation of the process job.
-
#ok_colorize(string) ⇒ String
The ok_colorize method applies color formatting to a string based on the success status.
-
#to_json ⇒ String
The to_json method converts the object to a JSON string representation.
-
#type ⇒ String
Returns the type identifier for the process job.
Constructor Details
#initialize(args:, probe_server: nil) ⇒ Utils::ProcessJob
Initializes a new ProcessJob instance with the specified arguments and optional probe server.
This method creates a process job object that can be enqueued for execution by a probe server. It assigns a unique job ID from the probe server if provided and stores the command arguments as an array.
to use for generating job IDs
the provided arguments and server reference
21 22 23 24 |
# File 'lib/utils/probe_server.rb', line 21 def initialize(args:, probe_server: nil) @id = probe_server&.next_job_id @args = Array(args) end |
Instance Attribute Details
#args ⇒ Array (readonly)
The args reader method provides access to the arguments stored in the instance.
35 36 37 |
# File 'lib/utils/probe_server.rb', line 35 def args @args end |
#id ⇒ Integer (readonly)
Returns the unique identifier of the process job.
29 30 31 |
# File 'lib/utils/probe_server.rb', line 29 def id @id end |
#ok ⇒ String
The ok method returns a character representation of the job’s success status.
This method provides a visual indicator of whether a process job has succeeded, failed, or is still in progress. It returns ‘y’ for successful jobs, ‘n’ for failed jobs, and ‘…’ for jobs that are currently running or pending.
the status is unknown
63 64 65 66 67 68 69 |
# File 'lib/utils/probe_server.rb', line 63 def ok case @ok when false then 'n' when true then 'y' else '…' end end |
Instance Method Details
#as_json ⇒ Hash
The as_json method converts the process job object into a JSON-serializable hash.
This method creates and returns a hash representation of the process job, containing its type, unique identifier, and command arguments.
112 113 114 |
# File 'lib/utils/probe_server.rb', line 112 def as_json(*) { type:, id:, args:, } end |
#inspect ⇒ String Also known as: to_s
The inspect method generates a colorized string representation of the process job.
This method creates a formatted string that includes the job’s unique identifier and its command arguments, with the status indicator color-coded based on whether the job succeeded, failed, or is pending.
99 100 101 |
# File 'lib/utils/probe_server.rb', line 99 def inspect ok_colorize("#{id} #{args.map { |a| a.include?(' ') ? a.inspect : a } * ' '}") end |
#ok_colorize(string) ⇒ String
The ok_colorize method applies color formatting to a string based on the success status.
This method returns the input string wrapped with color codes to indicate whether the associated process job succeeded, failed, or is in progress. Successful jobs are highlighted in green, failed jobs in red, and pending jobs are returned without any color formatting.
82 83 84 85 86 87 88 |
# File 'lib/utils/probe_server.rb', line 82 def ok_colorize(string) case @ok when false then white { on_red { string } } when true then black { on_green { string } } else string end end |
#to_json ⇒ String
The to_json method converts the object to a JSON string representation.
This method delegates to the as_json method to generate a hash representation of the object, then converts that hash to a JSON string using the standard JSON library’s to_json method.
123 124 125 |
# File 'lib/utils/probe_server.rb', line 123 def to_json(*) as_json.to_json(*) end |
#type ⇒ String
Returns the type identifier for the process job.
This method provides a constant string value that identifies the object as a process job within the probe server system, facilitating type-based dispatch and handling.
49 50 51 |
# File 'lib/utils/probe_server.rb', line 49 def type 'process_job' end |