Module: SurveyorRedcapParserAnswerMethods

Defined in:
lib/surveyor/redcap_parser.rb

Overview

Answer model

Instance Method Summary collapse

Instance Method Details

#build_and_set(context, r) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/surveyor/redcap_parser.rb', line 201

def build_and_set(context, r)
  case r[:field_type]
  when "text"
    self.attributes = {
      :response_class => "string",
      :text => "Text",
      :display_order => context[:question].answers.size }
    context[:question].answers << context[:answer] = self
  when "notes"
    self.attributes = {
      :response_class => "text",
      :text => "Notes",
      :display_order => context[:question].answers.size }
    context[:question].answers << context[:answer] = self
  when "file"
    Surveyor::RedcapParser.rake_trace "\n!!! skipping answer: file"
  end
  (r[:choices_or_calculations] || r[:choices_calculations_or_slider_labels]).to_s.split("|").each do |pair|
    aref, atext = pair.split(",").map(&:strip)
    if aref.blank? or atext.blank? or (aref.to_i.to_s != aref)
      Surveyor::RedcapParser.rake_trace "\n!!! skipping answer #{pair}"
    else
      a = Answer.new({
        :reference_identifier => aref,
        :text => atext,
        :display_order => context[:question].answers.size })
      context[:question].answers << context[:answer] = a
      unless context[:question].reference_identifier.blank? or aref.blank? or !context[:answer].valid?
        context[:answer_references] ||= {}
        context[:answer_references][context[:question].reference_identifier] ||= {}
        context[:answer_references][context[:question].reference_identifier][aref] = context[:answer]
      end
      Surveyor::RedcapParser.rake_trace "#{context[:answer].errors.full_messages}, #{context[:answer].inspect}" unless context[:answer].valid?
      Surveyor::RedcapParser.rake_trace "answer_#{context[:answer].reference_identifier} "
    end
  end
end