class ActiveModel::Validations::EncodableValidator

Public Instance Methods

check_validity!() click to toggle source
# File lib/active_model/validators/encodable_validator.rb, line 6
def check_validity!
  values = options[:encodings]
  unless values.respond_to?(:all?) and values.all? {|v| v.class <= Encoding} 
    raise ArgumentError, ":encodings must be a array of Encodings"
  end
end
validate_each(record, attr_name, value) click to toggle source
# File lib/active_model/validators/encodable_validator.rb, line 13
def validate_each(record, attr_name, value)
  return if options[:allow_nil] && value.nil?

  unless value.respond_to?(:encode)
    record.errors.add(attr_name, :not_a_encodable_object , options)
    return
  end

  src_enc = value.encoding
  last_enc = nil
  begin
    options[:encodings].each do |enc|
      last_enc = enc
      value.encode(enc, src_enc)
    end
  rescue Encoding::UndefinedConversionError
    record.errors.add(attr_name, :can_not_be_encoded, options.merge(
      :value => value,
      :encoding => last_enc
    ))
    return
  rescue Encoding::InvalidByteSequenceError
    record.errors.add(attr_name, :included_invalid_byte_sequences, options)
    return
  end
end