Sha256: b5d388e36779603e89963d5bed76d4ad6ac5ade9da158c061f539970ee035dd5

Contents?: true

Size: 1.32 KB

Versions: 9

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module Journal
  class Section
    attr_accessor :key, :title, :questions, :answers, :condition

    ##
    ## Initializes the given section.
    ##
    ## @param      section  [Hash] The section as defined in
    ##                      configuration
    ##
    ## @return     [Section] the configured section
    ##
    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

    ##
    ## Ask the questions detailed in the 'questions' section of the configuration
    ##
    ## @return [Hash] the question responses
    ##
    def ask_questions
      @questions.each do |question|
        if 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
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
journal-cli-1.0.31 lib/journal-cli/section.rb
journal-cli-1.0.30 lib/journal-cli/section.rb
journal-cli-1.0.29 lib/journal-cli/section.rb
journal-cli-1.0.28 lib/journal-cli/section.rb
journal-cli-1.0.27 lib/journal-cli/section.rb
journal-cli-1.0.26 lib/journal-cli/section.rb
journal-cli-1.0.25 lib/journal-cli/section.rb
journal-cli-1.0.24 lib/journal-cli/section.rb
journal-cli-1.0.23 lib/journal-cli/section.rb