Class: Journal::Question

Inherits:
Object
  • Object
show all
Defined in:
lib/journal-cli/question.rb

Overview

Individual question

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 min and max



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

#conditionObject (readonly)

Returns the value of attribute condition.



6
7
8
# File 'lib/journal-cli/question.rb', line 6

def condition
  @condition
end

#gumObject (readonly)

Returns the value of attribute gum.



6
7
8
# File 'lib/journal-cli/question.rb', line 6

def gum
  @gum
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/journal-cli/question.rb', line 6

def key
  @key
end

#maxObject (readonly)

Returns the value of attribute max.



6
7
8
# File 'lib/journal-cli/question.rb', line 6

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



6
7
8
# File 'lib/journal-cli/question.rb', line 6

def min
  @min
end

#promptObject (readonly)

Returns the value of attribute prompt.



6
7
8
# File 'lib/journal-cli/question.rb', line 6

def prompt
  @prompt
end

#secondary_promptObject (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

#typeObject (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

Returns:

  • (Number, String)

    the response 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