Class: Reorm::MaximumLengthValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/reorm/validators/maximum_length_validator.rb

Instance Attribute Summary

Attributes inherited from Validator

#field, #message

Instance Method Summary collapse

Constructor Details

#initialize(length, *field) ⇒ MaximumLengthValidator

Returns a new instance of MaximumLengthValidator.



7
8
9
10
# File 'lib/reorm/validators/maximum_length_validator.rb', line 7

def initialize(length, *field)
  super("is too long (maximum length is #{length} characters).", *field)
  @length = length
end

Instance Method Details

#validate(object) ⇒ Object



12
13
14
15
16
17
# File 'lib/reorm/validators/maximum_length_validator.rb', line 12

def validate(object)
  value = field.value(object)
  if value && value.to_s.length > @length
    object.errors.add field.to_s, message
  end
end