Class: Surveyor::RedcapParser

Inherits:
Object
  • Object
show all
Defined in:
lib/surveyor/redcap_parser.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRedcapParser

Instance methods



24
25
26
27
# File 'lib/surveyor/redcap_parser.rb', line 24

def initialize
  self.context = {}
  self.context[:dependency_conditions] = []
end

Class Attribute Details

.optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/surveyor/redcap_parser.rb', line 6

def options
  @options
end

Instance Attribute Details

#contextObject

Attributes



9
10
11
# File 'lib/surveyor/redcap_parser.rb', line 9

def context
  @context
end

Class Method Details

.parse(str, filename, options = {}) ⇒ Object

Class methods



12
13
14
15
16
17
# File 'lib/surveyor/redcap_parser.rb', line 12

def self.parse(str, filename, options={})
  self.options = options
  Surveyor::RedcapParser.rake_trace "\n"
  Surveyor::RedcapParser.new.parse(str, filename)
  Surveyor::RedcapParser.rake_trace "\n"
end

.rake_trace(str) ⇒ Object



18
19
20
21
# File 'lib/surveyor/redcap_parser.rb', line 18

def self.rake_trace(str)
  self.options ||= {}
  print str if self.options[:trace] == true
end

Instance Method Details

#missing_columns(r) ⇒ Object



53
54
55
56
57
58
# File 'lib/surveyor/redcap_parser.rb', line 53

def missing_columns(r)
  missing = []
  missing << "choices_or_calculations" unless r.map(&:to_s).include?("choices_or_calculations") or r.map(&:to_s).include?("choices_calculations_or_slider_labels")
  missing << "text_validation_type" unless r.map(&:to_s).include?("text_validation_type") or r.map(&:to_s).include?("text_validation_type_or_show_slider_number")
  missing += (static_required_columns - r.map(&:to_s))
end

#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

#resolve_referencesObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/surveyor/redcap_parser.rb', line 63

def resolve_references
  context[:dependency_conditions].each do |dc|
    Surveyor::RedcapParser.rake_trace "resolve(#{dc.question_reference},#{dc.answer_reference})"
    if dc.answer_reference.blank? and (context[:question_references][dc.question_reference].answers.size == 1)
      Surveyor::RedcapParser.rake_trace "...found "
      dc.question = context[:question_references][dc.question_reference]
      dc.answer = dc.question.answers.first
    elsif answer = context[:answer_references][dc.question_reference][dc.answer_reference]
      Surveyor::RedcapParser.rake_trace "...found "
      dc.answer = answer
      dc.question = context[:question_references][dc.question_reference]
    else
      Surveyor::RedcapParser.rake_trace "\n!!! failed lookup for dependency_condition q: #{question_reference} a: #{question_reference}"
    end
  end
end

#static_required_columnsObject



59
60
61
62
# File 'lib/surveyor/redcap_parser.rb', line 59

def static_required_columns
  # no longer requiring field_units
  %w(variable__field_name form_name section_header field_type field_label field_note text_validation_min text_validation_max identifier branching_logic_show_field_only_if required_field)
end