Class: Validation

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Surveyor::Models::ValidationMethods
Defined in:
lib/surveyor/parser.rb,
app/models/validation.rb,
lib/surveyor/unparser.rb,
lib/surveyor/redcap_parser.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Surveyor::Models::ValidationMethods

#conditions_hash, included, #is_valid?

Class Method Details

+ (Object) build_and_set(context, r)



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
238
239
240
241
# File 'lib/surveyor/redcap_parser.rb', line 201

def self.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|
      context[:validation] = a.validations.build(:rule => min ? max ? "A and B" : "A" : "B")
      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|
        context[:validation] = a.validations.build(:rule => "A")
        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|
        context[:validation] = a.validations.build(:rule => "A")
        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|
        context[:validation] = a.validations.build(:rule => "A")
        context[:validation].validation_conditions.build(:rule_key => "A", :operator => "=~", :regexp => "^\d*(,\d{3})*(\.\d*)?$")
      end
    when "phone"
      context[:question].answers.each do |a|
        context[:validation] = a.validations.build(:rule => "A")
        context[:validation].validation_conditions.build(:rule_key => "A", :operator => "=~", :regexp => "\d{3}.*\d{4}")
      end
    end
  end
end

+ (Object) parse_and_build(context, args, original_method, reference_identifier)

nonblock



268
269
270
271
272
273
274
# File 'lib/surveyor/parser.rb', line 268

def self.parse_and_build(context, args, original_method, reference_identifier)
  # clear context
  context.delete_if{|k,v| %w(validation validation_condition).map(&:to_sym).include? k}

  # build and set context
  context[:validation] = context[:answer].validations.build({:rule => "A"}.merge(args[0] || {}))
end

Instance Method Details

- (Object) unparse(dsl)

nonblock



128
129
130
131
132
133
134
# File 'lib/surveyor/unparser.rb', line 128

def unparse(dsl)
  attrs = (self.attributes.diff Validation.new.attributes).delete_if{|k,v| %w(created_at updated_at id answer_id).include?(k) }.symbolize_keys!
  dsl << "  " if answer.question.part_of_group?
  dsl << "    validation"
  dsl << (attrs.blank? ? "\n" : " #{attrs.inspect.gsub(/\{|\}/, "")}\n")
  validation_conditions.each{|validation_condition| validation_condition.unparse(dsl)}
end