Sha256: 67708b473ecbdea5b5c9c3a73be3cd144ef491504e93e4797cf30dae7670f504

Contents?: true

Size: 1020 Bytes

Versions: 7

Compression:

Stored size: 1020 Bytes

Contents

# frozen_string_literal: true

require "tty/prompt"

module RailsInteractive
  # 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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rails-interactive-1.0.0 lib/rails_interactive/prompt.rb
rails-interactive-0.1.9 lib/rails_interactive/prompt.rb
rails-interactive-0.1.8 lib/rails_interactive/prompt.rb
rails-interactive-0.1.7 lib/rails_interactive/prompt.rb
rails-interactive-0.1.6 lib/rails_interactive/prompt.rb
rails-interactive-0.1.5 lib/rails_interactive/prompt.rb
rails-interactive-0.1.4 lib/rails_interactive/prompt.rb