1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
# frozen_string_literal: true
require "forwardable"
require_relative "choice"
module TTY
class Prompt
# A class responsible for storing a collection of choices
#
# @api private
class Choices
include Enumerable
extend Forwardable
def_delegators :choices, :length, :size, :to_ary, :empty?,
:values_at, :index, :==
# Convenience for creating choices
#
# @param [Array[Object]] choices
# the choice objects
#
# @return [Choices]
# the choices collection
#
# @api public
def self.[](*choices)
new(choices)
end
# Create Choices collection
#
# @param [Array[Choice]] choices
# the choices to add to collection
#
# @api public
def initialize(choices = [])
@choices = choices.map do |choice|
Choice.from(choice)
end
end
# Scope of choices which are not disabled
#
# @api public
def enabled
reject(&:disabled?)
end
def enabled_indexes
each_with_index.reduce([]) do |acc, (choice, idx)|
acc << idx unless choice.disabled?
acc
end
end
# Iterate over all choices in the collection
#
# @yield [Choice]
#
# @api public
def each(&block)
return to_enum unless block_given?
choices.each(&block)
end
# Add choice to collection
#
# @param [Object] choice
# the choice to add
#
# @api public
def <<(choice)
choices << Choice.from(choice)
end
# Access choice by index
#
# @param [Integer] index
#
# @return [Choice]
#
# @api public
def [](index)
@choices[index]
end
# Pluck a choice by its name from collection
#
# @param [String] name
# the label name for the choice
#
# @return [Choice]
#
# @api public
def pluck(name)
map { |choice| choice.public_send(name) }
end
# Find a matching choice
#
# @example
# choices.find_by(:name, "small")
#
# @param [Symbol] attr
# the attribute name
# @param [Object] value
#
# @return [Choice]
#
# @api public
def find_by(attr, value)
find { |choice| choice.public_send(attr) == value }
end
protected
# The actual collection choices
#
# @return [Array[Choice]]
#
# @api private
attr_reader :choices
end # Choices
end # Prompt
end # TTY
|