Sha256: 2244d1935789cf65f17092b1157bd5526b04daa620f4d2fe2d7842caddaf9cff

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module Prompts
  class Form
    def self.submit(&block)
      instance = new()
      yield instance if block_given?
      instance.submit
    end

    def initialize
      @content = Prompts::Content.new
      @prompts = []
      @results = []
    end

    def content(&block)
      yield @content
      @content
    end

    def text(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil, &block)
      prompt = TextPrompt.new(label: label, prompt: prompt, hint: hint, default: default, required: required, validate: validate)
      yield(prompt) if block_given?
      prepend_form_content_to_prompt(prompt)
      @prompts << prompt
    end

    def select(label: nil, options: nil, prompt: "> ", hint: nil, default: nil, validate: nil, &block)
      prompt = SelectPrompt.new(label: label, options: options, prompt: prompt, hint: hint, default: default, validate: validate)
      yield(prompt) if block_given?
      prepend_form_content_to_prompt(prompt)
      @prompts << prompt
    end

    def pause(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil, &block)
      prompt = PausePrompt.new(label: label, prompt: prompt, hint: hint, default: default, required: required, validate: validate)
      yield(prompt) if block_given?
      prepend_form_content_to_prompt(prompt)
      @prompts << prompt
    end

    def confirm(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil, &block)
      prompt = ConfirmPrompt.new(label: label, prompt: prompt, hint: hint, default: default, required: required, validate: validate)
      yield(prompt) if block_given?
      prepend_form_content_to_prompt(prompt)
      @prompts << prompt
    end

    def submit
      @prompts.each do |prompt|
        @results << prompt.ask
      end
      @results
    end

    private

    def prepend_form_content_to_prompt(prompt)
      prompt.prepare_content
      @content.gap
      prompt.prepend_content(*@content.slots)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prompts-0.2.1 lib/prompts/form.rb