Class: TTY::Prompt::Choices Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/tty/prompt/choices.rb

Overview

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.

A class responsible for storing a collection of choices

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(choices = []) ⇒ Choices

Create Choices collection

Parameters:

  • choices (Array[Choice]) (defaults to: [])

    the choices to add to collection



44
45
46
47
48
# File 'lib/tty/prompt/choices.rb', line 44

def initialize(choices = [])
  @choices = choices.map do |choice|
    Choice.from(choice)
  end
end

Instance Attribute Details

#choicesArray[Choice] (readonly)

The actual collection choices

Returns:



21
22
23
# File 'lib/tty/prompt/choices.rb', line 21

def choices
  @choices
end

Class Method Details

.[](*choices) ⇒ Choices

Convenience for creating choices

Parameters:

  • choices (Array[Object])

    the choice objects

Returns:

  • (Choices)

    the choices collection



34
35
36
# File 'lib/tty/prompt/choices.rb', line 34

def self.[](*choices)
  new(choices)
end

Instance Method Details

#<<(choice) ⇒ Object

Add choice to collection

Parameters:

  • choice (Object)

    the choice to add



66
67
68
# File 'lib/tty/prompt/choices.rb', line 66

def <<(choice)
  choices << Choice.from(choice)
end

#[](index) ⇒ Choice

Access choice by index

Parameters:

  • index (Integer)

Returns:



77
78
79
# File 'lib/tty/prompt/choices.rb', line 77

def [](index)
  @choices[index]
end

#each {|Choice| ... } ⇒ Object

Iterate over all choices in the collection

Yields:



55
56
57
58
# 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) ⇒ Choice

Find a matching choice

Parameters:

  • attr (Symbol)

    the attribute name

  • value (Object)

Returns:



105
106
107
# File 'lib/tty/prompt/choices.rb', line 105

def find_by(attr, value)
  find { |choice| choice.public_send(attr) == value }
end

#pluck(name) ⇒ Choice

Pluck a choice by its name from collection

Parameters:

  • name (String)

    the label name for the choice

Returns:



89
90
91
# File 'lib/tty/prompt/choices.rb', line 89

def pluck(name)
  map { |choice| choice.public_send(name) }
end