Class: Planter::Prompt::Question

Inherits:
Object
  • Object
show all
Defined in:
lib/planter/prompt.rb

Overview

Class to prompt for answers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(question) ⇒ Question

Initializes the given question.

Parameters:

  • question (Hash)

    The question with key, prompt, and type, optionally default, min and max



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/planter/prompt.rb', line 18

def initialize(question)
  @key = question[:key].to_var
  @type = question[:type].normalize_type
  @min = question[:min]&.to_f || 1.0
  @max = question[:max]&.to_f || 10.0
  @prompt = question[:prompt] || nil
  @default = question[:default]&.to_s&.apply_all || nil
  @value = question[:value]
  @choices = question[:choices] || []
  @date_format = question[:date_format] || nil
  @gum = false # TTY::Which.exist?('gum')
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



8
9
10
# File 'lib/planter/prompt.rb', line 8

def condition
  @condition
end

#defaultObject (readonly)

Returns the value of attribute default.



8
9
10
# File 'lib/planter/prompt.rb', line 8

def default
  @default
end

#gumObject (readonly)

Returns the value of attribute gum.



8
9
10
# File 'lib/planter/prompt.rb', line 8

def gum
  @gum
end

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/planter/prompt.rb', line 8

def key
  @key
end

#maxObject (readonly)

Returns the value of attribute max.



8
9
10
# File 'lib/planter/prompt.rb', line 8

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



8
9
10
# File 'lib/planter/prompt.rb', line 8

def min
  @min
end

#promptObject (readonly)

Returns the value of attribute prompt.



8
9
10
# File 'lib/planter/prompt.rb', line 8

def prompt
  @prompt
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/planter/prompt.rb', line 8

def type
  @type
end

Instance Method Details

#askNumber, String

Ask the question, prompting for input based on type

Returns:

  • (Number, String)

    the response based on @type



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/planter/prompt.rb', line 36

def ask
  return nil if @prompt.nil?

  return @value.to_s.apply_all.coerce(@type) if @value && @type != :date

  res = case @type
        when :choice
          Prompt.choice(@choices, @prompt, default_response: @default)
        when :integer
          read_number(integer: true)
        when :float
          read_number
        when :date
          if @value
            date_default
          else
            read_date
          end
        when :class || :module
          read_line.to_class_name
        when :multiline
          read_lines
        else
          read_line
        end
  Planter.notify("{dw}#{prompt} => {dy}#{res}{x}", :debug, newline: false)
  res.to_s.apply_all
rescue TTY::Reader::InputInterrupt
  die('Canceled')
end