Class: Validatable::ValidatesLengthOf

Inherits:
ValidationBase show all
Defined in:
lib/validatable/validations/validates_length_of.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Attributes inherited from ValidationBase

#after_validate, #allow_blank, #allow_nil, #attribute, #if, #klass

Instance Method Summary collapse

Methods inherited from ValidationBase

#initialize, #validate

Constructor Details

This class inherits a constructor from Validatable::ValidationBase

Instance Attribute Details

#inObject

Returns the value of attribute in.



3
4
5
# File 'lib/validatable/validations/validates_length_of.rb', line 3

def in
  @in
end

#isObject

Returns the value of attribute is.



3
4
5
# File 'lib/validatable/validations/validates_length_of.rb', line 3

def is
  @is
end

#maximumObject

Returns the value of attribute maximum.



3
4
5
# File 'lib/validatable/validations/validates_length_of.rb', line 3

def maximum
  @maximum
end

#minimumObject

Returns the value of attribute minimum.



3
4
5
# File 'lib/validatable/validations/validates_length_of.rb', line 3

def minimum
  @minimum
end

Instance Method Details

#message(instance) ⇒ Object



5
6
7
# File 'lib/validatable/validations/validates_length_of.rb', line 5

def message(instance)
  super || "is invalid"
end

#valid?(instance) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/validatable/validations/validates_length_of.rb', line 9

def valid?(instance)
  valid = true
  value = instance.send(self.attribute)

  if value.nil?
    return true if allow_nil
    value = ''
  end

  if !value or (value.respond_to?(:empty?) and value.empty?)
    return true if allow_blank
    value = ''
  end

  valid &&= value.length <= maximum unless maximum.nil?
  valid &&= value.length >= minimum unless minimum.nil?
  valid &&= value.length == is unless is.nil?
  valid &&= self.in.include?(value.length) unless self.in.nil?
  valid
end