class Smartdown::Api::PreviousQuestionPage

Attributes

elements[R]
responses[R]
title[R]

Public Class Methods

new(node, responses) click to toggle source
# File lib/smartdown/api/previous_question_page.rb, line 9
def initialize(node, responses)
  node_elements = node.elements.clone
  headings = node_elements.select {
    |element| element.is_a? Smartdown::Model::Element::MarkdownHeading
  }
  nb_questions = node_elements.select{ |element|
    element.class.to_s.include?("Smartdown::Model::Element::Question")
  }.count
  if headings.count > nb_questions
    node_elements.delete(headings.first) #Remove page title
    @title = headings.first.content.to_s
  end
  @elements = node_elements
  @responses = responses
end

Public Instance Methods

answers() click to toggle source
# File lib/smartdown/api/previous_question_page.rb, line 25
def answers
  questions.map(&:answer)
end
questions() click to toggle source
# File lib/smartdown/api/previous_question_page.rb, line 29
def questions
  @questions ||= elements.slice_before do |element|
    element.is_a? Smartdown::Model::Element::MarkdownHeading
  end.select { |question_element_group| 
    question_element_group.any? { 
      |element| element.class.to_s.include?("Smartdown::Model::Element::Question")
    }
  }.each_with_index.map do |question_element_group, index|
    Smartdown::Api::PreviousQuestion.new(question_element_group, responses[index])
  end.compact
end