Class: Journal::Section

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(section) ⇒ Section

Initializes the given section.

Parameters:

  • section (Hash)

    The section as defined in configuration



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

#answersObject

Returns the value of attribute answers.



5
6
7
# File 'lib/journal-cli/section.rb', line 5

def answers
  @answers
end

#conditionObject

Returns the value of attribute condition.



5
6
7
# File 'lib/journal-cli/section.rb', line 5

def condition
  @condition
end

#keyObject

Returns the value of attribute key.



5
6
7
# File 'lib/journal-cli/section.rb', line 5

def key
  @key
end

#questionsObject

Returns the value of attribute questions.



5
6
7
# File 'lib/journal-cli/section.rb', line 5

def questions
  @questions
end

#titleObject

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_questionsHash

Ask the questions detailed in the ‘questions’ section of the configuration

Returns:

  • (Hash)

    the question responses



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