class ProcessExecuter::Options::Base

@api public
end
e.message #=> “option1 must be a String but was 1noption2 must be a String but was 2”
rescue ProcessExecuter::ArgumentError => e
options = MyOptions.new(option1: 1, option2: 2)
begin
@example invalid option values
options.option2 # => ‘value2’
options.option1 # => ‘value1’
options = MyOptions.new(option1: ‘value1’, option2: ‘value2’)
end
end
errors << “#{key} must be a String but was #{value}”
return if value.is_a?(String)
def assert_is_string(key, value)
end
]
)
:option3, default: ”, validator: method(:assert_is_string)
ProcessExecuter::Options::OptionDefinition.new(
),
:option2, default: ”, validator: method(:assert_is_string)
ProcessExecuter::Options::OptionDefinition.new(
),
:option1, default: ”, validator: method(:assert_is_string)
ProcessExecuter::Options::OptionDefinition.new(
*super,
[
# Call super to include options defined in the parent class
def define_options
class MyOptions < ProcessExecuter::Options::Base
@example Define an options class with two options
Options are defined by subclasses by overriding the ‘define_options` method.
Defines, validates, and holds a set of option values

def allowed_options

Returns:
  • (Hash) - A hash
def allowed_options
  @allowed_options ||=
    define_options.each_with_object({}) do |option, hash|
      hash[option.name] = option
    end.freeze
end

def assert_no_unknown_options

Other tags:
    Api: - private

Raises:
  • (ProcessExecuter::ArgumentError) - if the options hash contains any unknown options

Returns:
  • (void) -
def assert_no_unknown_options
  unknown_options = options_hash.keys.reject { |key| valid_option?(key) }
  return if unknown_options.empty?
  raise(
    ArgumentError,
    "Unknown option#{'s' if unknown_options.count > 1}: #{unknown_options.join(', ')}"
  )
end

def define_accessor_methods

Other tags:
    Api: - private

Returns:
  • (void) -
def define_accessor_methods
  allowed_options.each_key do |option|
    define_singleton_method(option) do
      options_hash[option]
    end
  end
end

def define_options

Other tags:
    Api: - private

Returns:
  • (Array) -
def define_options
  [].freeze
end

def each_with_object(obj, &)

Returns:
  • (Object) - the obj passed to the block

Other tags:
    Yieldparam: obj - The object passed to the block.
    Yieldparam: key_value - An array containing the option key and its value

Other tags:
    Yield: -
def each_with_object(obj, &)
  options_hash.each_with_object(obj, &)
end

def initialize(**options_hash)

Parameters:
  • options_hash (Hash) -- a hash of options

Other tags:
    Example: with invalid option values -
def initialize(**options_hash)
  @options_hash = allowed_options.transform_values(&:default).merge(options_hash)
  @errors = []
  assert_no_unknown_options
  define_accessor_methods
  validate_options
end

def inspect

Returns:
  • (String) -
def inspect
  options_hash.inspect
end

def merge(*other_options_hashes)

Returns:
  • (self.class) -

Parameters:
  • other_options_hashes (Array) -- the options to merge into the current options
def merge(*other_options_hashes)
  merged_options = other_options_hashes.reduce(options_hash, :merge)
  self.class.new(**merged_options)
end

def merge!(*other_options_hashes)

Other tags:
    Api: - public

Returns:
  • (self) - the current options object with the merged options

Parameters:
  • other_options_hashes (Array) -- zero of more hashes to merge into the current options
def merge!(*other_options_hashes)
  options_hash.merge!(*other_options_hashes)
end

def to_h

Returns:
  • (Hash) -
def to_h
  options_hash.dup
end

def to_s

Returns:
  • (String) -
def to_s
  "#{super.to_s[0..-2]} #{inspect}>"
end

def valid_option?(option)

Other tags:
    Api: - private

Returns:
  • (Boolean) - true if the given option is a valid option

Parameters:
  • option (Symbol) -- the option to be tested
def valid_option?(option)
  allowed_options.keys.include?(option)
end

def validate_options

Other tags:
    Api: - private

Raises:
  • (ProcessExecuter::ArgumentError) - if any invalid option values are found

Returns:
  • (void) -
def validate_options
  options_hash.each_key do |option_key|
    validator = allowed_options[option_key]&.validator
    instance_exec(option_key, send(option_key), &validator.to_proc) unless validator.nil?
  end
  raise ProcessExecuter::ArgumentError, errors.join("\n") unless errors.empty?
end