Class: Planter::Prompt::Question
- Inherits:
-
Object
- Object
- Planter::Prompt::Question
- Defined in:
- lib/planter/prompt.rb
Overview
Class to prompt for answers
Instance Attribute Summary collapse
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#gum ⇒ Object
readonly
Returns the value of attribute gum.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#prompt ⇒ Object
readonly
Returns the value of attribute prompt.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#ask ⇒ Number, String
Ask the question, prompting for input based on type.
-
#initialize(question) ⇒ Question
constructor
Initializes the given question.
Constructor Details
#initialize(question) ⇒ Question
Initializes the given question.
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
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
8 9 10 |
# File 'lib/planter/prompt.rb', line 8 def condition @condition end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
8 9 10 |
# File 'lib/planter/prompt.rb', line 8 def default @default end |
#gum ⇒ Object (readonly)
Returns the value of attribute gum.
8 9 10 |
# File 'lib/planter/prompt.rb', line 8 def gum @gum end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
8 9 10 |
# File 'lib/planter/prompt.rb', line 8 def key @key end |
#max ⇒ Object (readonly)
Returns the value of attribute max.
8 9 10 |
# File 'lib/planter/prompt.rb', line 8 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
8 9 10 |
# File 'lib/planter/prompt.rb', line 8 def min @min end |
#prompt ⇒ Object (readonly)
Returns the value of attribute prompt.
8 9 10 |
# File 'lib/planter/prompt.rb', line 8 def prompt @prompt end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
8 9 10 |
# File 'lib/planter/prompt.rb', line 8 def type @type end |
Instance Method Details
#ask ⇒ Number, String
Ask the question, prompting for input 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 |