class TTY::Prompt::Choices

A class responsible for storing a collection of choices

@api private

Attributes

choices[R]

The actual collection choices

@return [Array]

@api private

Public Class Methods

[](*choices) click to toggle source

Convenience for creating choices

@param [Array] choices

the choice objects

@return [Choices]

the choices collection

@api public

# File lib/tty/prompt/choices.rb, line 27
def self.[](*choices)
  new(choices)
end
new(choices = []) click to toggle source

Create Choices collection

@param [Array] choices

the choices to add to collection

@api public

# File lib/tty/prompt/choices.rb, line 37
def initialize(choices = [])
  @choices = choices.map do |choice|
    Choice.from(choice)
  end
end

Public Instance Methods

<<(choice) click to toggle source

Add choice to collection

@param [Object] choice

the choice to add

@api public

# File lib/tty/prompt/choices.rb, line 74
def <<(choice)
  choices << Choice.from(choice)
end
[](index) click to toggle source

Access choice by index

@param [Integer] index

@return [Choice]

@api public

# File lib/tty/prompt/choices.rb, line 85
def [](index)
  @choices[index]
end
each(&block) click to toggle source

Iterate over all choices in the collection

@yield [Choice]

@api public

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

  choices.each(&block)
end
enabled() click to toggle source

Scope of choices which are not disabled

@api public

# File lib/tty/prompt/choices.rb, line 46
def enabled
  reject(&:disabled?)
end
enabled_indexes() click to toggle source
# File lib/tty/prompt/choices.rb, line 50
def enabled_indexes
  each_with_index.reduce([]) do |acc, (choice, idx)|
    acc << idx unless choice.disabled?
    acc
  end
end
find_by(attr, value) click to toggle source

Find a matching choice

@example

choices.find_by(:name, "small")

@param [Symbol] attr

the attribute name

@param [Object] value

@return [Choice]

@api public

# File lib/tty/prompt/choices.rb, line 113
def find_by(attr, value)
  find { |choice| choice.public_send(attr) == value }
end
pluck(name) click to toggle source

Pluck a choice by its name from collection

@param [String] name

the label name for the choice

@return [Choice]

@api public

# File lib/tty/prompt/choices.rb, line 97
def pluck(name)
  map { |choice| choice.public_send(name) }
end