Module: Bureaucrat::Validation::Validators

Includes:
Validates
Included in:
Fields::Field
Defined in:
lib/bureaucrat/validation.rb

Constant Summary collapse

EMAIL_RE =
/(^[-!#\$%&'*+\/=?^_`{}|~0-9A-Z]+(\.[-!#\$%&'*+\/=?^_`{}|~0-9A-Z]+)*|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*")@(?:[A-Z0-9]+(?:-*[A-Z0-9]+)*\.)+[A-Z]{2,6}$/i

Class Method Summary collapse

Methods included from Validates

fail_with

Class Method Details

.empty_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/bureaucrat/validation.rb', line 56

def empty_value?(value)
  value.nil? || value == ''
end

.has_max_decimal_places(values, decimal_places) ⇒ Object



101
102
103
104
105
106
# File 'lib/bureaucrat/validation.rb', line 101

def has_max_decimal_places(values, decimal_places)
  sign, alldigits, _, whole_digits = value.split
  decimals = alldigits.length - whole_digits
  fail_with(:max_decimal_places, :max => decimal_places) if
    decimals > decimal_places
end

.has_max_digits(value, max_digits) ⇒ Object



96
97
98
99
# File 'lib/bureaucrat/validation.rb', line 96

def has_max_digits(value, max_digits)
  sign, alldigits, _, whole_digits = value.split
  fail_with(:max_digits, :max => max_digits) if alldigits > max_digits
end

.has_max_length(value, max_length) ⇒ Object



82
83
84
85
86
# File 'lib/bureaucrat/validation.rb', line 82

def has_max_length(value, max_length)
  value_length = value.length
  fail_with(:max_length, :max => max_length,
            :length => value_length) if value_length > max_length
end

.has_max_whole_digits(value, max_digits) ⇒ Object



108
109
110
111
# File 'lib/bureaucrat/validation.rb', line 108

def has_max_whole_digits(value, max_digits)
  sign, alldigits, _, whole_digits = value.split
  fail_with(:max_digits, :max => max_digits) if alldigits > max_digits
end

.has_min_length(value, min_length) ⇒ Object



76
77
78
79
80
# File 'lib/bureaucrat/validation.rb', line 76

def has_min_length(value, min_length)
  value_length = value.length
  fail_with(:min_length, :min => min_length,
            :length => value_length) if value_length < min_length
end

.included_in(value, collection) ⇒ Object



113
114
115
116
# File 'lib/bureaucrat/validation.rb', line 113

def included_in(value, collection)
  fail_with(:not_included, :collection => collection) unless
    collection.include?(value)
end

.is_array(value) ⇒ Object



72
73
74
# File 'lib/bureaucrat/validation.rb', line 72

def is_array(value)
  fail_with(:invalid_list) unless value.kind_of(Array)
end

.is_email(value) ⇒ Object



124
125
126
# File 'lib/bureaucrat/validation.rb', line 124

def is_email(value)
  matches_regex(value, EMAIL_RE)
end

.is_not_greater_than(value, max_value) ⇒ Object



92
93
94
# File 'lib/bureaucrat/validation.rb', line 92

def is_not_greater_than(value, max_value)
  fail_with(:max_value, :max => max_value) if value > max_value
end

.is_not_lesser_than(value, min_value) ⇒ Object



88
89
90
# File 'lib/bureaucrat/validation.rb', line 88

def is_not_lesser_than(value, min_value)
  fail_with(:min_value, :min => min_value) if value < min_value
end

.is_present(value) ⇒ Object



60
61
62
# File 'lib/bureaucrat/validation.rb', line 60

def is_present(value)
  fail_with(:required) if empty_value?(value)
end

.is_true(value) ⇒ Object



68
69
70
# File 'lib/bureaucrat/validation.rb', line 68

def is_true(value)
  fail_with(:required) unless value
end

.matches_regex(value, regex, error_code = :invalid) ⇒ Object



118
119
120
# File 'lib/bureaucrat/validation.rb', line 118

def matches_regex(value, regex, error_code=:invalid)
  fail_with(error_code, :regex => regex) if regex !~ value
end

.not_empty(value) ⇒ Object



64
65
66
# File 'lib/bureaucrat/validation.rb', line 64

def not_empty(value)
  fail_with(:required) if value.empty?
end