Module: Clack::Core::SelectionManager
- 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
-
#selected_labels(all_options) ⇒ String
Build the final display string from selected options.
-
#toggle_value(value) ⇒ Object
Toggle a value in the selection set.
-
#update_selection_value ⇒ Object
Update @value from the current selection.
-
#validate_selection ⇒ Boolean
Validate that selection meets requirements before submit.
Instance Method Details
#selected_labels(all_options) ⇒ String
Build the final display string from selected options.
44 45 46 |
# File 'lib/clack/core/selection_manager.rb', line 44 def selected_labels() .select { |o| @selected.include?(o.value) }.map { |o| o.label }.join(", ") end |
#toggle_value(value) ⇒ Object
Toggle a value in the selection set.
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_value ⇒ Object
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_selection ⇒ Boolean
Validate that selection meets requirements before submit. Sets error state if required and empty.
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 |