Class: Geet::Utils::ManualListSelection

Inherits:
Object
  • Object
show all
Defined in:
lib/geet/utils/manual_list_selection.rb

Constant Summary collapse

NO_SELECTION_KEY =
'(none)'
PAGER_SIZE =
16

Instance Method Summary collapse

Instance Method Details

#select_entries(entry_type, entries, name_method: nil) ⇒ Object

Shows a prompt for selecting an entry from a list.

Returns an empty array, without showing the prompt, if there are no entries.

See #select_entry for the parameters.

returns: array of entries.



46
47
48
49
50
51
52
53
54
# File 'lib/geet/utils/manual_list_selection.rb', line 46

def select_entries(entry_type, entries, name_method: nil)
  return [] if entries.empty?

  check_entries(entries, entry_type)

  entries = create_entries_map(entries, name_method)

  show_prompt(:multi_select, entry_type, entries)
end

#select_entry(entry_type, entries, name_method: nil) ⇒ Object

Shows a prompt for selecting an entry from a list.

Returns nil, without showing the prompt, if there are no entries.

entry_type: description of the entries type. entries: array of objects; if they’re not strings, must also pass :name_method.

this value must not be empty.

name_method: required when non-string objects are passed as entries; its invocation on

each object must return a string, which is used as key.

returns: the selected entry. if the null entry (NO_SELECTION_KEY) is selected, nil is

returned.


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/geet/utils/manual_list_selection.rb', line 25

def select_entry(entry_type, entries, name_method: nil)
  return nil if entries.empty?

  check_entries(entries, entry_type)

  entries = create_entries_map(entries, name_method)
  entries = add_no_selection_entry(entries)

  chosen_entry = show_prompt(:select, entry_type, entries)

  no_selection?(chosen_entry) ? nil : chosen_entry
end