Method: Surveyor::RedcapParser#parse

Defined in:
lib/surveyor/redcap_parser.rb

#parse(str, filename) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/surveyor/redcap_parser.rb', line 28

def parse(str, filename)
  csvlib = Surveyor::Common.csv_impl
  begin
    csvlib.parse(str, :headers => :first_row, :return_headers => true, :header_converters => :symbol) do |r|
      if r.header_row? # header row
        return Surveyor::RedcapParser.rake_trace "Missing headers: #{missing_columns(r.headers).inspect}\n\n" unless missing_columns(r.headers).blank?
        context[:survey] = Survey.new(:title => filename)
        Surveyor::RedcapParser.rake_trace "survey_#{context[:survey].access_code} "
      else # non-header rows
        SurveySection.new.extend(SurveyorRedcapParserSurveySectionMethods).build_or_set(context, r)
        Question.new.extend(SurveyorRedcapParserQuestionMethods).build_and_set(context, r)
        Answer.new.extend(SurveyorRedcapParserAnswerMethods).build_and_set(context, r)
        Validation.new.extend(SurveyorRedcapParserValidationMethods).build_and_set(context, r)
        Dependency.new.extend(SurveyorRedcapParserDependencyMethods).build_and_set(context, r)
      end
    end
    resolve_references
    Surveyor::RedcapParser.rake_trace context[:survey].save ? "saved. " : " not saved! #{context[:survey].errors.full_messages.join(", ")} "
    # Surveyor::RedcapParser.rake_trace context[:survey].sections.map(&:questions).flatten.map(&:answers).flatten.map{|x| x.errors.each_full{|y| y}.join}.join
  rescue csvlib::MalformedCSVError
    raise Surveyor::RedcapParserError, "Oops. Not a valid CSV file."
  # ensure
  end
  return context[:survey]
end