class TTY::Prompt::Choices
A class responsible for storing a collection of choices
@api private
Attributes
choices[R]
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 28 def self.[](*choices) new(choices) 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 75 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 86 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 63 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 47 def enabled reject(&:disabled?) end
enabled_indexes()
click to toggle source
# 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)
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 114 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 98 def pluck(name) map { |choice| choice.public_send(name) } end