Sha256: 2d4874ca6fcaa293b3967849a42259f2e7d515803507f6cfd111984fd2ef008f

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require "tty/prompt"

module RailsInteractive
  class CLI
    # Prompt class for commands
    class Prompt
      # Create a new instance
      #
      # @param msg [String] the message to display
      # @param type [String] the type of prompt
      # @param options [Array] the options to display
      # @param required [Boolean] whether the prompt value is required
      #
      # @return [Interactive::Prompt] the new instance
      def initialize(msg, type, options = nil, required: false)
        @msg = msg
        @type = type
        @options = options
        @required = required
        @prompt = TTY::Prompt.new
      end

      # Perform the prompt
      #
      # @return [String] the value of the prompt
      def perform
        case @type
        when "ask"
          @prompt.ask(@msg, required: @required)
        when "select"
          @prompt.select(@msg, @options, required: @required)
        when "multi_select"
          @prompt.multi_select(@msg, @options)
        else
          puts "Invalid parameter"
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails-interactive-2.1.3 lib/cli/prompt.rb
rails-interactive-2.1.2 lib/cli/prompt.rb
rails-interactive-2.1.1 lib/cli/prompt.rb
rails-interactive-2.1.0 lib/cli/prompt.rb
rails-interactive-2.0.0 lib/cli/prompt.rb