class GuidedPath::Parser::Spec1

Constants

MULTIMEDIA_CATEGORIES

Public Class Methods

new(options = {}) click to toggle source
# File lib/guided_path/parser/spec1.rb, line 4
def initialize(options = {})
  @program = options[:program] || raise(ArgumentError, "Cannot run a parser without a program specified")
  @script = preprocess_script(options[:script])
  @script_name = options[:script_name] || raise(ArgumentError, 'Must specify a script name')
  parse_script(@script)
end

Private Instance Methods

add_node(options = {}) click to toggle source
# File lib/guided_path/parser/spec1.rb, line 82
def add_node(options = {})
  @program.add_node({:script_name => @script_name}.merge(options))
end
find_content_node(script) click to toggle source
# File lib/guided_path/parser/spec1.rb, line 39
def find_content_node(script)

  new_node = nil
  new_node ||= find_goto_node(script)
  new_node ||= find_lead_in_node(script)
  new_node ||= find_question_node(script)
  new_node ||= find_multimedia_node(script)
  new_node ||= find_simple_textnode(script)
  new_node ||= find_hash_textnode(script)
  return new_node
end
find_goto_node(script) click to toggle source
# File lib/guided_path/parser/spec1.rb, line 73
def find_goto_node(script)

  if script.kind_of?(Hash) && (next_node = (script["goto"] || script["next_node"]))
    GotoNode.new(next_node: next_node)
  else
    nil
  end
end
find_hash_textnode(script) click to toggle source
# File lib/guided_path/parser/spec1.rb, line 189
def find_hash_textnode(script)
  if script.kind_of?(Hash) && (text_node = script['text'])
    new_node = find_simple_textnode(text_node)
    new_node ||= TextNode.new(text_node) if text_node.kind_of?(Hash)
    return new_node
  end
end
find_lead_in_node(script) click to toggle source
# File lib/guided_path/parser/spec1.rb, line 51
def find_lead_in_node(script)

  if script.kind_of?(Hash) && (next_node = (script["lead_in"]))
    LeadInNode.new(next_node)
  else
    nil
  end
end
find_multimedia_node(script) click to toggle source
# File lib/guided_path/parser/spec1.rb, line 61
def find_multimedia_node(script)

  if script.kind_of?(Hash) 
    if (multimedia_category = script.keys.detect { |k| MULTIMEDIA_CATEGORIES.include?(k) })
      mm_params = script[multimedia_category]
      mm_params['source'] = multimedia_category
      new_node ||= MultimediaNode.new(mm_params)
      return new_node
    end
  end
end
find_question_node(script) click to toggle source
# File lib/guided_path/parser/spec1.rb, line 155
def find_question_node(script)
  return nil unless script.kind_of?(Hash) 

  if (question_hash = (script['q'] || script['question']))
    case question_hash['type']
    when 'yes/no'
      parse_question_yes_no(script)
    when 'multiple choice' 
      parse_question_multiple_choice(script)
    when 'textbox'
      parse_question_textbox(script)
    else
      parse_question_multiple_choice(script)
    end
  elsif script['multiple choice q'] || script['multiple choice question']
    parse_question_multiple_choice(script)
  elsif script['yes/no q'] || script['yes/no question']
    parse_question_yes_no(script)
  elsif script['textbox q'] || script['textbox question']
    parse_question_textbox(script)
  else
    nil
  end
end
find_simple_textnode(script) click to toggle source
# File lib/guided_path/parser/spec1.rb, line 181
def find_simple_textnode(script)
  if script.kind_of?(String) || script.kind_of?(Symbol)
    return TextNode.new(value: script)
  end

  return nil
end
parse_question_multiple_choice(script) click to toggle source
# File lib/guided_path/parser/spec1.rb, line 137
def parse_question_multiple_choice(script)
  no_start = @program.starting_node.nil?

  raise "Question formed invalidly. Did you indent the question enough?\n\n#{script.inspect}" unless script.values.length == 1
  question_script = script.values.first
  raise "Question node should be a hash underneath." unless question_script.kind_of?(Hash)
  raise "No answers node found in:\n\n#{script.inspect}" unless question_script.has_key?('answers')
  question_script['answers'].each do |name, subscript| 
    question_script['answers'][name] = parse_script(subscript)
  end
  new_node = QuestionMultipleChoiceNode.new(question_script)
  if no_start
    @program.starting_node = new_node
  end

  return new_node
end
parse_question_textbox(script) click to toggle source
# File lib/guided_path/parser/spec1.rb, line 87
def parse_question_textbox(script)

  no_start = @program.starting_node.nil?
  raise "Question formed invalidly. Did you indent the question enough?\n\n#{script.inspect}" unless script.values.length == 1
  question_script = script.values.first
  raise "Question node should be a hash underneath." unless question_script.kind_of?(Hash)

  question_script['answers']['~not listed~'] ||=  "Sorry, the answer you gave was not listed."
  question_script['answers']['~blank~'] ||=  "Sorry, you must provide an answer."
  question_script['answers'].each do |name, subscript|
    question_script['answers'][name] = parse_script(subscript)
  end

  new_node = QuestionTextBoxNode.new(question_script)

  if no_start
    @program.starting_node = new_node
  end

  return new_node
end
parse_question_yes_no(script) click to toggle source
# File lib/guided_path/parser/spec1.rb, line 108
def parse_question_yes_no(script)
  no_start = @program.starting_node.nil?

  raise "Question formed invalidly. Did you indent the question enough?\n\n#{script.inspect}" unless script.values.length == 1
  question_script = script.values.first
  raise "Question node should be a hash underneath." unless question_script.kind_of?(Hash)

  yes_label = ['yes', 'Yes', 'true', 'True', true].detect { |label| question_script.has_key?(label)} 
  no_label = ['no','No','false','False', false].detect { |label| question_script.has_key?(label)} 

  raise "No yes node found" if yes_label.nil?
  raise "No no node found" if no_label.nil?

  question_script['answers'] = {
    'Yes' => parse_script(question_script[yes_label]),
    'No' => parse_script(question_script[no_label])
  }


  new_node = QuestionMultipleChoiceNode.new(question_script)

  if no_start
    @program.starting_node = new_node
  end

  return new_node

end
parse_script(script) click to toggle source
# File lib/guided_path/parser/spec1.rb, line 13
def parse_script(script)
  if (node = find_content_node(script))
    return add_node(node:node)  
  elsif script.kind_of?(Array)
    parse_sequence_node(script)
  end
end
parse_sequence_node(script) click to toggle source
# File lib/guided_path/parser/spec1.rb, line 21
def parse_sequence_node(script)
  last_node = nil
  last_added_nodes = []
  script.each do |node|
    existing_nodes = @program.nodes.keys
    new_node = parse_script(node)
    added_nodes = @program.nodes.reject { |label, n| existing_nodes.include?(label) || n.next_node }
    
    last_added_nodes.each { |label, n| n.next_node = new_node }

    last_node = new_node
    last_added_nodes = added_nodes
  end


  return last_node
end
preprocess_script(raw_script) click to toggle source
# File lib/guided_path/parser/spec1.rb, line 198
def preprocess_script(raw_script)
  raise "Must specify script" unless raw_script 
  YAML.load(raw_script)
end