Module: SurveyorRedcapParserValidationMethods

Defined in:
lib/surveyor/redcap_parser.rb

Overview

Validation model

Instance Method Summary collapse

Instance Method Details

#build_and_set(context, r) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/surveyor/redcap_parser.rb', line 243

def build_and_set(context, r)
  # text_validation_type text_validation_min text_validation_max
  min = r[:text_validation_min].to_s.blank? ? nil : r[:text_validation_min].to_s
  max = r[:text_validation_max].to_s.blank? ? nil : r[:text_validation_max].to_s
  type = r[:text_validation_type].to_s.blank? ? nil : r[:text_validation_type].to_s
  if min or max
    context[:question].answers.each do |a|
      self.rule = (min ? max ? "A and B" : "A" : "B")
      a.validations << context[:validation] = self
      context[:validation].validation_conditions.build(:rule_key => "A", :operator => ">=", :integer_value => min) if min
      context[:validation].validation_conditions.build(:rule_key => "B", :operator => "<=", :integer_value => max) if max
    end
  elsif type
    # date email integer number phone
    case r[:text_validation_type]
    when "date"
      context[:question].display_type = :date if context[:question].display_type == :string
    when "email"
      context[:question].answers.each do |a|
        self.rule = "A"
        a.validations << context[:validation] = self
        context[:validation].validation_conditions.build(:rule_key => "A", :operator => "=~", :regexp => "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$")
      end
    when "integer"
      context[:question].display_type = :integer if context[:question].display_type == :string
      context[:question].answers.each do |a|
        self.rule = "A"
        a.validations << context[:validation] = self
        context[:validation].validation_conditions.build(:rule_key => "A", :operator => "=~", :regexp => "\d+")
      end
    when "number"
      context[:question].display_type = :float if context[:question].display_type == :string
      context[:question].answers.each do |a|
        self.rule = "A"
        a.validations << context[:validation] = self
        context[:validation].validation_conditions.build(:rule_key => "A", :operator => "=~", :regexp => "^\d*(,\d{3})*(\.\d*)?$")
      end
    when "phone"
      context[:question].answers.each do |a|
                  self.rule = "A"
        a.validations << context[:validation] = self
        context[:validation].validation_conditions.build(:rule_key => "A", :operator => "=~", :regexp => "\d{3}.*\d{4}")
      end
    end
  end
end