class TTY::Prompt::SelectedChoices

@api private

Attributes

size[R]

Public Class Methods

new(selected = [], indexes = []) click to toggle source

Create selected choices

@param [Array<Choice>] selected @param [Array<Integer>] indexes

@api public

# File lib/tty/prompt/selected_choices.rb, line 16
def initialize(selected = [], indexes = [])
  @selected = selected
  @indexes = indexes
  @size = @selected.size
end

Public Instance Methods

clear() click to toggle source

Clear selected choices

@api public

# File lib/tty/prompt/selected_choices.rb, line 25
def clear
  @indexes.clear
  @selected.clear
  @size = 0
end
delete_at(index) click to toggle source

Delete choice at index

@return [Choice]

the deleted choice

@api public

# File lib/tty/prompt/selected_choices.rb, line 61
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) click to toggle source

Iterate over selected choices

@api public

# File lib/tty/prompt/selected_choices.rb, line 34
def each(&block)
  return to_enum unless block_given?

  @selected.each(&block)
end
find_index_by(&search) click to toggle source
# File lib/tty/prompt/selected_choices.rb, line 71
def find_index_by(&search)
  (0...@size).bsearch(&search)
end
insert(index, choice) click to toggle source

Insert choice at index

@param [Integer] index @param [Choice] choice

@api public

# File lib/tty/prompt/selected_choices.rb, line 46
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