class ClassValidator

Public Instance Methods

validate_each(record, attribute, values) click to toggle source
# File lib/thingtank/validators.rb, line 21
def validate_each(record, attribute, values)
  case values
  when NilClass
    return nil
  when Array
    if options[:with] && options[:with] == Array
      validate_single_class(record, attribute, values, options)
    else
      values.each do |value|
        validate_single_class(record, attribute, value, options)
      end
    end
  else
    validate_single_class(record, attribute, values, options)
  end
end
validate_single_class(record, attribute, values, options) click to toggle source
# File lib/thingtank/validators.rb, line 2
def validate_single_class(record, attribute, values, options)
  if options[:in] # validates :hosting_account, :class => [Hash, String]
    valid = false
    options[:in].each do |klass|
      if values.is_a?(klass)
        valid = true
      end
    end
    unless valid
      record.errors.add attribute, "invalid class is #{values.class.to_s}, should be one of #{options[:in].join(', ')}"
    end
  else # validates :hosting_account, :class => Hash
    unless values.is_a?(options[:with])
      record.errors.add attribute, "invalid class is #{values.class.to_s}, should be #{options[:with]}"
    end
  end
end