Module: Clack::Core::SelectionManager

Included in:
Prompts::AutocompleteMultiselect, Prompts::GroupMultiselect, Prompts::Multiselect
Defined in:
lib/clack/core/selection_manager.rb

Overview

Shared selection management for multi-select prompts. Handles toggle, toggle-all, required validation, and value tracking.

Including classes must define:

  • @selected [Set] the set of selected values

  • @required [Boolean] whether at least one selection is required

Constant Summary collapse

REQUIRED_ERROR =
"Please select at least one option. Press %s to select, %s to submit"

Instance Method Summary collapse

Instance Method Details

#selected_labels(all_options) ⇒ String

Build the final display string from selected options.

Parameters:

  • all_options (Array<Hash>)

    the full options list to match against

Returns:

  • (String)

    comma-separated labels



44
45
46
# File 'lib/clack/core/selection_manager.rb', line 44

def selected_labels(all_options)
  all_options.select { |o| @selected.include?(o.value) }.map { |o| o.label }.join(", ")
end

#toggle_value(value) ⇒ Object

Toggle a value in the selection set.

Parameters:

  • value (Object)

    the value to toggle



16
17
18
19
20
21
22
# File 'lib/clack/core/selection_manager.rb', line 16

def toggle_value(value)
  if @selected.include?(value)
    @selected.delete(value)
  else
    @selected.add(value)
  end
end

#update_selection_valueObject

Update @value from the current selection.



37
38
39
# File 'lib/clack/core/selection_manager.rb', line 37

def update_selection_value
  @value = @selected.to_a
end

#validate_selectionBoolean

Validate that selection meets requirements before submit. Sets error state if required and empty.

Returns:

  • (Boolean)

    true if valid to proceed with submit



27
28
29
30
31
32
33
34
# File 'lib/clack/core/selection_manager.rb', line 27

def validate_selection
  if @required && @selected.empty?
    @error_message = format(REQUIRED_ERROR, Colors.cyan("space"), Colors.cyan("enter"))
    @state = :error
    return false
  end
  true
end