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

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



38
39
40
41
42
# File 'lib/tty/prompt/choices.rb', line 38

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

Class Method Details

.[](*choices) ⇒ Choices

Convenience for creating choices

Parameters:

  • choices (Array[Object])

    the choice objects

Returns:

  • (Choices)

    the choices collection



28
29
30
# File 'lib/tty/prompt/choices.rb', line 28

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

Instance Method Details

#<<(choice) ⇒ Object

Add choice to collection

Parameters:

  • choice (Object)

    the choice to add



75
76
77
# File 'lib/tty/prompt/choices.rb', line 75

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

#[](index) ⇒ Choice

Access choice by index

Parameters:

  • index (Integer)

Returns:



86
87
88
# File 'lib/tty/prompt/choices.rb', line 86

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

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

Iterate over all choices in the collection

Yields:



63
64
65
66
67
# File 'lib/tty/prompt/choices.rb', line 63

def each(&block)
  return to_enum unless block_given?

  choices.each(&block)
end

#enabledObject

Scope of choices which are not disabled



47
48
49
# File 'lib/tty/prompt/choices.rb', line 47

def enabled
  reject(&:disabled?)
end

#enabled_indexesObject

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.



51
52
53
54
55
56
# File 'lib/tty/prompt/choices.rb', line 51

def enabled_indexes
  each_with_index.reduce([]) do |acc, (choice, idx)|
    acc << idx unless choice.disabled?
    acc
  end
end

#find_by(attr, value) ⇒ Choice

Find a matching choice

Examples:

choices.find_by(:name, "small")

Parameters:

  • attr (Symbol)

    the attribute name

  • value (Object)

Returns:



114
115
116
# File 'lib/tty/prompt/choices.rb', line 114

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:



98
99
100
# File 'lib/tty/prompt/choices.rb', line 98

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