class Smartdown::Api::Flow

Attributes

scenario_sets[R]

Public Class Methods

new(smartdown_input, options = {}) click to toggle source
# File lib/smartdown/api/flow.rb, line 15
def initialize(smartdown_input, options = {})
  initial_state = options.fetch(:initial_state, {})
  data_module = options.fetch(:data_module, nil)

  @smartdown_flow = Smartdown::Parser::FlowInterpreter.new(smartdown_input, data_module).interpret
  @engine = Smartdown::Engine.new(@smartdown_flow, initial_state)
  @scenario_sets = Smartdown::Parser::ScenarioSetsInterpreter.new(smartdown_input).interpret
end

Public Instance Methods

content_id() click to toggle source
# File lib/smartdown/api/flow.rb, line 49
def content_id
  front_matter.fetch :content_id, nil
end
coversheet() click to toggle source
# File lib/smartdown/api/flow.rb, line 84
def coversheet
  @coversheet ||= Smartdown::Api::Coversheet.new(@smartdown_flow.coversheet)
end
draft?() click to toggle source
# File lib/smartdown/api/flow.rb, line 57
def draft?
  status == 'draft'
end
meta_description() click to toggle source
# File lib/smartdown/api/flow.rb, line 41
def meta_description
  front_matter.fetch :meta_description, nil
end
name() click to toggle source
# File lib/smartdown/api/flow.rb, line 33
def name
  @smartdown_flow.name
end
need_id() click to toggle source
# File lib/smartdown/api/flow.rb, line 45
def need_id
  front_matter.fetch :satisfies_need, nil
end
nodes() click to toggle source
# File lib/smartdown/api/flow.rb, line 69
def nodes
  @smartdown_flow.nodes.map{ |node| transform_node(node) }
    .select{ |node| (node.is_a? Smartdown::Api::QuestionPage) ||
                    (node.is_a? Smartdown::Api::Outcome)
  }
end
outcomes() click to toggle source
# File lib/smartdown/api/flow.rb, line 80
def outcomes
  nodes.select{ |node| node.is_a? Smartdown::Api::Outcome}
end
published?() click to toggle source
# File lib/smartdown/api/flow.rb, line 65
def published?
  status == 'published'
end
question_pages() click to toggle source
# File lib/smartdown/api/flow.rb, line 76
def question_pages
  nodes.select{ |node| node.is_a? Smartdown::Api::QuestionPage }
end
state(started, responses) click to toggle source
# File lib/smartdown/api/flow.rb, line 24
def state(started, responses)
  state = smartdown_state(started, responses)
  State.new(transform_node(evaluate_node(node_by_name(state.get(:current_node)), state)),
            previous_question_nodes_for(state),
            state.get(:accepted_responses)[1..-1] || [],
            state.get(:current_answers)
  )
end
status() click to toggle source
# File lib/smartdown/api/flow.rb, line 53
def status
  front_matter.status
end
title() click to toggle source
# File lib/smartdown/api/flow.rb, line 37
def title
  coversheet.title
end
transition?() click to toggle source
# File lib/smartdown/api/flow.rb, line 61
def transition?
  status == 'transition'
end

Private Instance Methods

evaluate_node(node, state) click to toggle source
# File lib/smartdown/api/flow.rb, line 104
def evaluate_node(node, state)
  Smartdown::Engine::NodePresenter.new.present(node, state)
end
front_matter() click to toggle source
# File lib/smartdown/api/flow.rb, line 108
def front_matter
  @front_matter ||= coversheet.front_matter
end
node_by_name(node_name) click to toggle source
# File lib/smartdown/api/flow.rb, line 120
def node_by_name(node_name)
  @smartdown_flow.node(node_name)
end
previous_question_nodes_for(state) click to toggle source
# File lib/smartdown/api/flow.rb, line 124
def previous_question_nodes_for(state)
  node_path = state.get('path')
  return [] if node_path.empty?

  node_path[1..-1].map do |node_name|
    evaluate_node(node_by_name(node_name), state)
  end
end
smartdown_state(started, responses) click to toggle source
# File lib/smartdown/api/flow.rb, line 112
def smartdown_state(started, responses)
  smartdown_responses = responses.clone
  if started
    smartdown_responses.unshift('y')
  end
  @engine.process(smartdown_responses)
end
transform_node(node) click to toggle source
# File lib/smartdown/api/flow.rb, line 90
def transform_node(node)
  if node.is_start_page_node?
    Smartdown::Api::Coversheet.new(node)
  elsif node.is_question_node?
    if node.elements.any?{|element| element.class.to_s.include?("Smartdown::Model::Element::Question")}
      Smartdown::Api::QuestionPage.new(node)
    else
      raise("Unknown node type: #{node.elements.map(&:class)}")
    end
  else
    Smartdown::Api::Outcome.new(node)
  end
end