Class: ProcessExecuter::Options::SpawnOptions

Inherits:
Base
  • Object
show all
Defined in:
lib/process_executer/options/spawn_options.rb

Overview

Validate Process.spawn options and return Process.spawn options

Allow subclasses to add additional options that are not passed to Process.spawn

Valid options are those accepted by Process.spawn.

Direct Known Subclasses

SpawnAndWaitOptions

Constant Summary collapse

SPAWN_OPTIONS =

:nocov: SimpleCov on JRuby reports hashes declared on multiple lines as not covered

[
  OptionDefinition.new(:unsetenv_others, default: :not_set),
  OptionDefinition.new(:pgroup, default: :not_set),
  OptionDefinition.new(:new_pgroup, default: :not_set),
  OptionDefinition.new(:rlimit_resourcename, default: :not_set),
  OptionDefinition.new(:umask, default: :not_set),
  OptionDefinition.new(:close_others, default: :not_set),
  OptionDefinition.new(:chdir, default: :not_set)
].freeze

Instance Attribute Summary

Attributes inherited from Base

#errors

Instance Method Summary collapse

Methods inherited from Base

#allowed_options, #each_with_object, #initialize, #inspect, #merge!, #to_h, #to_s, #with

Constructor Details

This class inherits a constructor from ProcessExecuter::Options::Base

Instance Method Details

#redirection?(option_key) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine if the given option key indicates a redirection option

Parameters:

  • option_key (Symbol, Integer, IO, Array)

    the option key to be tested

Returns:

  • (Boolean)
[View source]

49
50
51
52
# File 'lib/process_executer/options/spawn_options.rb', line 49

def redirection?(option_key)
  test = ->(key) { %i[in out err].include?(key) || key.is_a?(Integer) || (key.is_a?(IO) && !key.fileno.nil?) }
  test.call(option_key) || (option_key.is_a?(Array) && option_key.all? { |key| test.call(key) })
end

#spawn_optionsHash

Returns the options to be passed to Process.spawn

Examples:

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

Returns:

  • (Hash)
[View source]

37
38
39
40
41
42
43
# File 'lib/process_executer/options/spawn_options.rb', line 37

def spawn_options
  {}.tap do |spawn_options|
    options.each do |option_key, value|
      spawn_options[option_key] = value if include_spawn_option?(option_key, value)
    end
  end
end

#std_redirection?(option_key, symbol, fileno) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Does option_key indicate a standard redirection such as stdin, stdout, or stderr

Parameters:

  • option_key (Symbol, Integer, IO, Array)

    the option key to be tested

  • symbol (:in, :out, :err)

    the symbol to test for

  • fileno (Integer)

    the file descriptor number to test for

Returns:

  • (Boolean)
[View source]

60
61
62
63
# File 'lib/process_executer/options/spawn_options.rb', line 60

def std_redirection?(option_key, symbol, fileno)
  test = ->(key) { key == symbol || key == fileno || (key.is_a?(IO) && key.fileno == fileno) }
  test.call(option_key) || (option_key.is_a?(Array) && option_key.any? { |key| test.call(key) })
end

#stderr_redirection?(option_key) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine if the given option key indicates a redirection option for stderr

Parameters:

  • option_key (Symbol, Integer, IO, Array)

    the option key to be tested

Returns:

  • (Boolean)
[View source]

89
# File 'lib/process_executer/options/spawn_options.rb', line 89

def stderr_redirection?(option_key) = std_redirection?(option_key, :err, 2)

#stderr_redirection_keySymbol, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine the option key that indicates a redirection option for stderr

Returns:

  • (Symbol, Integer, IO, Array, nil)

    nil if not found

[View source]

94
95
96
# File 'lib/process_executer/options/spawn_options.rb', line 94

def stderr_redirection_key
  options.keys.find { |option_key| option_key if stderr_redirection?(option_key) }
end

#stderr_redirection_valueObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine the value of the redirection option for stderr

Returns:

  • (Object)
[View source]

101
102
103
# File 'lib/process_executer/options/spawn_options.rb', line 101

def stderr_redirection_value
  options[stderr_redirection_key]
end

#stdout_redirection?(option_key) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine if the given option key indicates a redirection option for stdout

Parameters:

  • option_key (Symbol, Integer, IO, Array)

    the option key to be tested

Returns:

  • (Boolean)
[View source]

69
# File 'lib/process_executer/options/spawn_options.rb', line 69

def stdout_redirection?(option_key) = std_redirection?(option_key, :out, 1)

#stdout_redirection_keySymbol, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine the option key that indicates a redirection option for stdout

Returns:

  • (Symbol, Integer, IO, Array, nil)

    nil if not found

[View source]

74
75
76
# File 'lib/process_executer/options/spawn_options.rb', line 74

def stdout_redirection_key
  options.keys.find { |option_key| option_key if stdout_redirection?(option_key) }
end

#stdout_redirection_valueObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine the value of the redirection option for stdout

Returns:

  • (Object)
[View source]

81
82
83
# File 'lib/process_executer/options/spawn_options.rb', line 81

def stdout_redirection_value
  options[stdout_redirection_key]
end