Class: Validatable::ValidatesLengthOf

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

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from ValidationBase

#attribute

Instance Method Summary collapse

Methods inherited from ValidationBase

after_validate, after_validations, all_after_validations, all_defaults, default, defaults, #i18n_prefix, #initialize, option, #raise_error_if_key_is_dup, required_option, #run_after_validate, #should_validate?, #validate_this_time?

Methods included from Requireable

included, #requires

Methods included from Understandable

included, #must_understand

Constructor Details

This class inherits a constructor from Validatable::ValidationBase

Instance Method Details

#i18nObject



9
10
11
# File 'lib/validatable/validations/validates_length_of.rb', line 9

def i18n
  super || "#{i18n_prefix}.wrong_length"
end

#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)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/validatable/validations/validates_length_of.rb', line 13

def valid?(instance)
  valid = true
  value = instance.send(self.attribute)
  
  if value.nil?
    return true if allow_nil
    value = ''
  end

  if value.blank?
    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 &&= within.include?(value.length) unless within.nil?
  valid
end