lib/tty/prompt/choices.rb in tty-prompt-0.21.0 vs lib/tty/prompt/choices.rb in tty-prompt-0.22.0
- old
+ new
@@ -1,29 +1,22 @@
# frozen_string_literal: true
-require 'forwardable'
+require "forwardable"
-require_relative 'choice'
+require_relative "choice"
module TTY
class Prompt
# A class responsible for storing a collection of choices
#
# @api private
class Choices
include Enumerable
extend Forwardable
- # The actual collection choices
- #
- # @return [Array[Choice]]
- #
- # @api public
- attr_reader :choices
-
def_delegators :choices, :length, :size, :to_ary, :empty?,
- :values_at, :index
+ :values_at, :index, :==
# Convenience for creating choices
#
# @param [Array[Object]] choices
# the choice objects
@@ -46,10 +39,24 @@
@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
@@ -92,11 +99,11 @@
end
# Find a matching choice
#
# @exmaple
- # choices.find_by(:name, 'small')
+ # choices.find_by(:name, "small")
#
# @param [Symbol] attr
# the attribute name
# @param [Object] value
#
@@ -104,8 +111,18 @@
#
# @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