Class: Reorm::MinimumLengthValidator

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

Instance Attribute Summary

Attributes inherited from Validator

#field, #message

Instance Method Summary collapse

Constructor Details

#initialize(length, *field) ⇒ MinimumLengthValidator

Returns a new instance of MinimumLengthValidator.



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

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

Instance Method Details

#validate(object) ⇒ Object



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

def validate(object)
  value = field.value(object)
  if [nil, ""].include?(value) || value.to_s.length < @length
    object.errors.add field.to_s, message
  end
end