Class: RiderServer::Validate::Integer

Inherits:
Base show all
Defined in:
lib/rider_server/validate/integer.rb

Instance Method Summary collapse

Methods inherited from Base

#fail, validate

Methods included from PredicateLogic

#add_predicate, #check_predicates

Instance Method Details

#default(value) ⇒ Object



15
16
17
18
# File 'lib/rider_server/validate/integer.rb', line 15

def default(value)
  @default = value
  self
end

#greater_than(value) ⇒ Object



20
21
22
23
# File 'lib/rider_server/validate/integer.rb', line 20

def greater_than(value)
  add_predicate { |data| [data > value, "must be greater than #{value}"] }
  self
end

#less_than(value) ⇒ Object



25
26
27
28
# File 'lib/rider_server/validate/integer.rb', line 25

def less_than(value)
  add_predicate { |data| [data < value, "must be less than #{value}"] }
  self
end

#validate(data, loc = "") ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rider_server/validate/integer.rb', line 30

def validate(data, loc = "")
  if data.nil? && @default
    data = @default
  elsif data.nil?
    fail(data, loc, "no default value set")
  end

  begin
    data = data.to_i
  rescue
    fail(data, loc, "can't convert to an integer")
  end

  result, reason = check_predicates(data)
  unless result
    fail(data, loc, reason)
  end

  data
end