Class: Journal::Question
- Inherits:
-
Object
- Object
- Journal::Question
- Defined in:
- lib/journal-cli/question.rb
Overview
Individual question
Instance Attribute Summary collapse
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#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.
-
#secondary_prompt ⇒ Object
readonly
Returns the value of attribute secondary_prompt.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#ask(condition) ⇒ 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.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/journal-cli/question.rb', line 15 def initialize(question) @key = question["key"] @type = question["type"] @min = question["min"]&.to_i || 1 @max = question["max"]&.to_i || 5 @prompt = question["prompt"] || nil @secondary_prompt = question["secondary_prompt"] || nil @gum = TTY::Which.exist?("gum") @condition = question.key?("condition") ? question["condition"].parse_condition : true end |
Instance Attribute Details
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
6 7 8 |
# File 'lib/journal-cli/question.rb', line 6 def condition @condition end |
#gum ⇒ Object (readonly)
Returns the value of attribute gum.
6 7 8 |
# File 'lib/journal-cli/question.rb', line 6 def gum @gum end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
6 7 8 |
# File 'lib/journal-cli/question.rb', line 6 def key @key end |
#max ⇒ Object (readonly)
Returns the value of attribute max.
6 7 8 |
# File 'lib/journal-cli/question.rb', line 6 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
6 7 8 |
# File 'lib/journal-cli/question.rb', line 6 def min @min end |
#prompt ⇒ Object (readonly)
Returns the value of attribute prompt.
6 7 8 |
# File 'lib/journal-cli/question.rb', line 6 def prompt @prompt end |
#secondary_prompt ⇒ Object (readonly)
Returns the value of attribute secondary_prompt.
6 7 8 |
# File 'lib/journal-cli/question.rb', line 6 def secondary_prompt @secondary_prompt end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/journal-cli/question.rb', line 6 def type @type end |
Instance Method Details
#ask(condition) ⇒ Number, String
Ask the question, prompting for input based on type
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/journal-cli/question.rb', line 31 def ask(condition) return nil if @prompt.nil? return nil unless @condition && condition res = case @type when /^int/i read_number(integer: true) when /^(float|num)/i read_number when /^(text|string|line)/i read_line when /^(weather|forecast)/i Weather.new(Journal.config["weather_api"], Journal.config["zip"], Journal.config["temp_in"]) when /^multi/i read_lines when /^(date|time)/i read_date end Journal.notify("{dw}#{prompt}: {dy}#{res}{x}".x) res end |