Class: Journal::Section
- Inherits:
-
Object
- Object
- Journal::Section
- Defined in:
- lib/journal-cli/section.rb
Instance Attribute Summary collapse
-
#answers ⇒ Object
Returns the value of attribute answers.
-
#condition ⇒ Object
Returns the value of attribute condition.
-
#key ⇒ Object
Returns the value of attribute key.
-
#questions ⇒ Object
Returns the value of attribute questions.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
-
#ask_questions ⇒ Hash
Ask the questions detailed in the ‘questions’ section of the configuration.
-
#initialize(section) ⇒ Section
constructor
Initializes the given section.
Constructor Details
#initialize(section) ⇒ Section
Initializes the given section.
15 16 17 18 19 20 21 22 23 |
# File 'lib/journal-cli/section.rb', line 15 def initialize(section) @key = section["key"] @title = section["title"] @condition = section.key?("condition") ? section["condition"].parse_condition : true @questions = section["questions"].map { |question| Question.new(question) } @questions.delete_if { |q| q.prompt.nil? } @answers = {} ask_questions end |
Instance Attribute Details
#answers ⇒ Object
Returns the value of attribute answers.
5 6 7 |
# File 'lib/journal-cli/section.rb', line 5 def answers @answers end |
#condition ⇒ Object
Returns the value of attribute condition.
5 6 7 |
# File 'lib/journal-cli/section.rb', line 5 def condition @condition end |
#key ⇒ Object
Returns the value of attribute key.
5 6 7 |
# File 'lib/journal-cli/section.rb', line 5 def key @key end |
#questions ⇒ Object
Returns the value of attribute questions.
5 6 7 |
# File 'lib/journal-cli/section.rb', line 5 def questions @questions end |
#title ⇒ Object
Returns the value of attribute title.
5 6 7 |
# File 'lib/journal-cli/section.rb', line 5 def title @title end |
Instance Method Details
#ask_questions ⇒ Hash
Ask the questions detailed in the ‘questions’ section of the configuration
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/journal-cli/section.rb', line 30 def ask_questions @questions.each do |question| if /\./.match?(question.key) res = @answers keys = question.key.split(".") keys.each_with_index do |key, i| next if i == keys.count - 1 res[key] = {} unless res.key?(key) res = res[key] end res[keys.last] = question.ask(@condition) else @answers[question.key] = question.ask(@condition) end end end |