Class: TTY::Prompt::SelectedChoices Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tty/prompt/selected_choices.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(selected = [], indexes = []) ⇒ SelectedChoices

Create selected choices

Parameters:

  • selected (Array<Choice>) (defaults to: [])
  • indexes (Array<Integer>) (defaults to: [])


17
18
19
20
21
# File 'lib/tty/prompt/selected_choices.rb', line 17

def initialize(selected = [], indexes = [])
  @selected = selected
  @indexes = indexes
  @size = @selected.size
end

Instance Attribute Details

#sizeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
# File 'lib/tty/prompt/selected_choices.rb', line 9

def size
  @size
end

Instance Method Details

#clearObject

Clear selected choices



26
27
28
29
30
# File 'lib/tty/prompt/selected_choices.rb', line 26

def clear
  @indexes.clear
  @selected.clear
  @size = 0
end

#delete_at(index) ⇒ Choice

Delete choice at index

Returns:

  • (Choice)

    the deleted choice



62
63
64
65
66
67
68
69
70
# File 'lib/tty/prompt/selected_choices.rb', line 62

def delete_at(index)
  delete_idx = @indexes.each_index.find { |i| index == @indexes[i] }
  return nil unless delete_idx

  @indexes.delete_at(delete_idx)
  choice = @selected.delete_at(delete_idx)
  @size -= 1
  choice
end

#each(&block) ⇒ Object

Iterate over selected choices



35
36
37
38
39
# File 'lib/tty/prompt/selected_choices.rb', line 35

def each(&block)
  return to_enum unless block_given?

  @selected.each(&block)
end

#find_index_by(&search) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



72
73
74
# File 'lib/tty/prompt/selected_choices.rb', line 72

def find_index_by(&search)
  (0...@size).bsearch(&search)
end

#insert(index, choice) ⇒ Object

Insert choice at index

Parameters:

  • index (Integer)
  • choice (Choice)


47
48
49
50
51
52
53
54
# File 'lib/tty/prompt/selected_choices.rb', line 47

def insert(index, choice)
  insert_idx = find_index_by { |i| index < @indexes[i] }
  insert_idx ||= -1
  @indexes.insert(insert_idx, index)
  @selected.insert(insert_idx, choice)
  @size += 1
  self
end