Module: Assistant::InputBuilder::OptionalOption

Included in:
Assistant::InputBuilder
Defined in:
lib/assistant/input_builder/optional_option.rb

Overview

M7: explicit optional: flag handling. Validates the value and returns the canonical option hash (with :required derived from optional: false). Mirrors DefaultOption's shape so the #input call site stays one line per option family.

Instance Method Summary collapse

Instance Method Details

#apply_optional_option(options) ⇒ Object

M7: pure translation of the validated optional: value into the canonical required: flag used by downstream validator helpers. optional: false -> required: true; optional: true is left alone (no valid_require_<name>? is generated, matching the default). The original :optional key is retained in input_definitions for introspection. Non-mutating: callers receive a new hash when a translation is applied.



42
43
44
# File 'lib/assistant/input_builder/optional_option.rb', line 42

def apply_optional_option(options)
  options[:optional] == false ? options.merge(required: true) : options
end

#process_optional_option(name:, options:) ⇒ Hash

Validate the optional: keyword for an input and return the canonical option hash. optional: false is translated into required: true so downstream validators see a single flag.

Parameters:

  • name (Symbol)

    input name

  • options (Hash)

    options hash from the #input call

Returns:

  • (Hash)

    the (possibly translated) options hash

Raises:

  • (ArgumentError)

    when optional: is non-boolean or contradicts required: true



16
17
18
19
# File 'lib/assistant/input_builder/optional_option.rb', line 16

def process_optional_option(name:, options:)
  validate_optional!(name:, options:)
  apply_optional_option(options)
end

#validate_optional!(name:, options:) ⇒ Object

M7: optional: must be a boolean. optional: true together with required: true is a contradiction. Both rules raise ArgumentError at class-definition time, before any method is generated.

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
# File 'lib/assistant/input_builder/optional_option.rb', line 25

def validate_optional!(name:, options:)
  optional = options[:optional]
  unless [true, false].include?(optional)
    raise ArgumentError, "optional: for input :#{name} must be true or false, got #{optional.inspect}"
  end
  return unless optional == true && options[:required] == true

  raise ArgumentError, "input :#{name} cannot be both required: true and optional: true"
end