Class: Surveyor::RedcapParser

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

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (RedcapParser) initialize

Instance methods



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

def initialize
  self.context = {}
end

Instance Attribute Details

- (Object) context

Attributes



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

def context
  @context
end

Class Method Details

+ (Object) parse(str, filename)

Class methods



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

def self.parse(str, filename)
  puts
  Surveyor::RedcapParser.new.parse(str, filename)
  puts
  puts
end

Instance Method Details

- (Object) missing_columns(r)



46
47
48
# File 'lib/surveyor/redcap_parser.rb', line 46

def missing_columns(r)
  required_columns - r.headers.map(&:to_s)
end

- (Object) parse(str, filename)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/surveyor/redcap_parser.rb', line 22

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

- (Object) required_columns



49
50
51
# File 'lib/surveyor/redcap_parser.rb', line 49

def required_columns
  %w(variable__field_name form_name field_units section_header field_type field_label choices_or_calculations field_note text_validation_type text_validation_min text_validation_max identifier branching_logic_show_field_only_if required_field)
end