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 public

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 34
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 44
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 66
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 77
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 55
def each(&block)
  return to_enum unless block_given?
  choices.each(&block)
end
find_by(attr, value) click to toggle source

Find a matching choice

@exmaple

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 105
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 89
def pluck(name)
  map { |choice| choice.public_send(name) }
end