Class: Vmodel::IntegerValidator

Inherits:
Validator show all
Defined in:
lib/vmodel/validators/integer_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(attr, options) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vmodel/validators/integer_validator.rb', line 3

def validate(attr, options)
  if attr.nil? && options[:required]
    return [false, "is required"]
  end
  if attr.nil? && options[:default]
    attr = options[:default]
  end
  if options[:cast]
    attr = Integer(attr)
  end
  return [false, "is not an integer"] if !attr.is_a?(Integer)
  options.each do |k,v|
    case k
    when :gte
      return [false, "should be great than #{v} or equal to #{v}"] if attr < v
    when :lte
      return [false, "should be less than #{v} or equal to #{v}"] if attr > v
    when :gt
      return [false, "should be great than #{v}"] if attr <= v
    when :lt
      return [false, "should be less than #{v}"] if attr >= v
    end
  end
end