Class: DocumentValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/thingtank/validators.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, values) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/thingtank/validators.rb', line 90

def validate_each(record, attribute, values)
  case values
  when NilClass
    return nil
  when Array
    values.each do |value|
      validate_single_doc(record, attribute, value, options)
    end
  else
    validate_single_doc(record, attribute, values, options)
  end
end

#validate_single_doc(record, attribute, value, options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/thingtank/validators.rb', line 78

def validate_single_doc(record, attribute, value, options)
  if options[:inline]
    return record.errors.add attribute, "#{value.inspect} should be an inlined doc that is converted to a hash" unless value.is_a? Hash
    record.errors.add attribute, character.errors.messages unless ThingTank.new(value).valid?
  else
    match = (value =~ /^\!\#(.+)$/)
    doc_id = $1
    return record.errors.add attribute, "#{value.inspect} does not begin with !# and is therefor no doc id" unless match      
    record.errors.add attribute, "doc with id #{doc_id} does not exist" unless record._doc.class.get(doc_id)
  end
end