Sha256: 3e17f50869f401fc586854cf925b31681241f5ec6434dddf148f524c33c74a12

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module Gamefic
  module Props
    # Props for MultipleChoice scenes.
    #
    class MultipleChoice < Default
      # A message to send the player for an invalid choice. A formatting
      # token named %<input>s can be used to inject the user input.
      #
      # @return [String]
      attr_writer :invalid_message

      # The array of available options.
      #
      # @return [Array<String>]
      def options
        @options ||= []
      end

      def invalid_message
        @invalid_message ||= '"%<input>s" is not a valid choice.'
      end

      # The zero-based index of the selected option.
      #
      # @return [Integer, nil]
      def index
        return nil unless input

        @index ||= index_by_number || index_by_text
      end

      # The one-based index of the selected option.
      #
      # @return [Integer, nil]
      def number
        return nil unless index

        index + 1
      end

      # The full text of the selected option.
      #
      # @return [String, nil]
      def selection
        return nil unless index

        options[index]
      end

      private

      def index_by_number
        return input.to_i - 1 if input.match(/^\d+$/) && options[input.to_i - 1]

        nil
      end

      def index_by_text
        options.find_index { |text| input.downcase == text.downcase }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gamefic-3.2.0 lib/gamefic/props/multiple_choice.rb
gamefic-3.1.0 lib/gamefic/props/multiple_choice.rb
gamefic-3.0.0 lib/gamefic/props/multiple_choice.rb