Class: TTY2::Prompt::Choices Private
- Inherits:
-
Object
- Object
- TTY2::Prompt::Choices
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/tty2/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
-
.[](*choices) ⇒ Choices
Convenience for creating choices.
Instance Method Summary collapse
-
#<<(choice) ⇒ Object
Add choice to collection.
-
#[](index) ⇒ Choice
Access choice by index.
-
#each {|Choice| ... } ⇒ Object
Iterate over all choices in the collection.
-
#enabled ⇒ Object
Scope of choices which are not disabled.
- #enabled_indexes ⇒ Object private
-
#find_by(attr, value) ⇒ Choice
Find a matching choice.
-
#initialize(choices = []) ⇒ Choices
constructor
Create Choices collection.
-
#pluck(name) ⇒ Choice
Pluck a choice by its name from collection.
Constructor Details
Class Method Details
.[](*choices) ⇒ Choices
Convenience for creating choices
28 29 30 |
# File 'lib/tty2/prompt/choices.rb', line 28 def self.[](*choices) new(choices) end |
Instance Method Details
#<<(choice) ⇒ Object
Add choice to collection
75 76 77 |
# File 'lib/tty2/prompt/choices.rb', line 75 def <<(choice) choices << Choice.from(choice) end |
#[](index) ⇒ Choice
Access choice by index
86 87 88 |
# File 'lib/tty2/prompt/choices.rb', line 86 def [](index) @choices[index] end |
#each {|Choice| ... } ⇒ Object
Iterate over all choices in the collection
63 64 65 66 67 |
# File 'lib/tty2/prompt/choices.rb', line 63 def each(&block) return to_enum unless block_given? choices.each(&block) end |
#enabled ⇒ Object
Scope of choices which are not disabled
47 48 49 |
# File 'lib/tty2/prompt/choices.rb', line 47 def enabled reject(&:disabled?) end |
#enabled_indexes ⇒ Object
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/tty2/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
114 115 116 |
# File 'lib/tty2/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
98 99 100 |
# File 'lib/tty2/prompt/choices.rb', line 98 def pluck(name) map { |choice| choice.public_send(name) } end |