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:
-
‘:timeout`:
@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
@!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
Source
# File lib/process_executer/options.rb, line 86 def initialize(**options) assert_no_unknown_options(options) @options = DEFAULTS.merge(options) assert_timeout_is_valid end
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.
Public Instance Methods
Source
# File lib/process_executer/options.rb, line 100 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
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]
Private Instance Methods
Source
# File lib/process_executer/options.rb, line 128 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
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
Source
# File lib/process_executer/options.rb, line 137 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
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
Source
# File lib/process_executer/options.rb, line 164 def include_spawn_option?(option, value) (option.is_a?(Integer) || option.is_a?(IO) || SPAWN_OPTIONS.include?(option)) && value != NOT_SET end
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
Source
# File lib/process_executer/options.rb, line 147 def invalid_timeout_message "timeout must be nil or a real non-negative number but was #{options[:timeout].pretty_inspect}" end
The message to be used when raising an error for an invalid timeout @return [String] @api private
Source
# File lib/process_executer/options.rb, line 155 def valid_option?(option) ALL_OPTIONS.include?(option) || option.is_a?(Integer) || option.respond_to?(:fileno) end
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