class ProcessExecuter::Options

Validate ProcessExecuter::Executer#spawn options and return Process.spawn options

Valid options are those accepted by Process.spawn plus the following additions:

@api public

Constants

ALL_OPTIONS

All options allowed by this class

DEFAULTS

The default values for all options @return [Hash]

NON_SPAWN_OPTIONS

These options are allowed by ‘ProcessExecuter.spawn` but should NOT be passed to `Process.spawn`

NOT_SET

Any ‘SPAWN_OPTIONS` set to `NOT_SET` will not be passed to `Process.spawn`

SPAWN_OPTIONS

These options should be passed to ‘Process.spawn`

Additionally, any options whose key is an Integer or an IO object will be passed to ‘Process.spawn`.

Attributes

options[R]

@!attribute [r]

Options with values

All options have values. If an option is not given in the initializer, it will have the value ‘NOT_SET`.

@return [Hash<Symbol, Object>]

@api private

Public Class Methods

new(**options) click to toggle source

Create a new Options object

@example

options = ProcessExecuter::Options.new(out: $stdout, err: $stderr, timeout: 10)

@param options [Hash] Process.spawn options plus additional options listed below.

See [Process.spawn](https://ruby-doc.org/core/Process.html#method-c-spawn)
for a list of valid options that can be passed to `Process.spawn`.

@option options [Integer, Float, nil] :timeout

Number of seconds to wait for the process to terminate. Any number
may be used, including Floats to specify fractional seconds. A value of 0 or nil
will allow the process to run indefinitely.
# File lib/process_executer/options.rb, line 88
def initialize(**options)
  assert_no_unknown_options(options)
  @options = DEFAULTS.merge(options)
  assert_timeout_is_valid
end

Public Instance Methods

spawn_options() click to toggle source

Returns the options to be passed to Process.spawn

@example

options = ProcessExecuter::Options.new(out: $stdout, err: $stderr, timeout: 10)
options.spawn_options # => { out: $stdout, err: $stderr }

@return [Hash]

# File lib/process_executer/options.rb, line 102
def spawn_options
  {}.tap do |spawn_options|
    options.each do |option, value|
      spawn_options[option] = value if include_spawn_option?(option, value)
    end
  end
end

Private Instance Methods

assert_no_unknown_options(options) click to toggle source

Determine if the options hash contains any unknown options @param options [Hash] the hash of options @return [void] @raise [ArgumentError] if the options hash contains any unknown options @api private

# File lib/process_executer/options.rb, line 130
def assert_no_unknown_options(options)
  unknown_options = options.keys.reject { |key| valid_option?(key) }
  raise ArgumentError, "Unknown options: #{unknown_options.join(', ')}" unless unknown_options.empty?
end
assert_timeout_is_valid() click to toggle source

Raise an error if timeout is not a real non-negative number @return [void] @raise [ArgumentError] if timeout is not a real non-negative number @api private

# File lib/process_executer/options.rb, line 139
def assert_timeout_is_valid
  return if @options[:timeout].nil?
  return if @options[:timeout].is_a?(Numeric) && @options[:timeout].real? && !@options[:timeout].negative?

  raise ArgumentError, invalid_timeout_message
end
include_spawn_option?(option, value) click to toggle source

Determine if the given option should be passed to ‘Process.spawn` @param option [Symbol, Integer, IO] the option to be tested @param value [Object] the value of the option @return [Boolean] true if the given option should be passed to `Process.spawn` @api private

# File lib/process_executer/options.rb, line 166
def include_spawn_option?(option, value)
  (option.is_a?(Integer) || option.is_a?(IO) || SPAWN_OPTIONS.include?(option)) && value != NOT_SET
end
invalid_timeout_message() click to toggle source

The message to be used when raising an error for an invalid timeout @return [String] @api private

# File lib/process_executer/options.rb, line 149
def invalid_timeout_message
  "timeout must be nil or a real non-negative number but was #{options[:timeout].pretty_inspect}"
end
valid_option?(option) click to toggle source

Determine if the given option is a valid option @param option [Symbol] the option to be tested @return [Boolean] true if the given option is a valid option @api private

# File lib/process_executer/options.rb, line 157
def valid_option?(option)
  ALL_OPTIONS.include?(option) || option.is_a?(Integer) || option.respond_to?(:fileno)
end