Class: RiderServer::Validate::Boolean

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

Constant Summary collapse

TRUE_STRINGS =
["yes", "true", "on"]

Instance Method Summary collapse

Methods inherited from Base

#fail, validate

Methods included from PredicateLogic

#add_predicate, #check_predicates

Instance Method Details

#default(value) ⇒ Object



17
18
19
20
# File 'lib/rider_server/validate/boolean.rb', line 17

def default(value)
  @default = value
  self
end

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rider_server/validate/boolean.rb', line 22

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

  case data

  when ::TrueClass, ::FalseClass
    data
  when ::String
    TRUE_STRINGS.member? data.downcase
  when ::Integer
    data > 0
  else
    fail(data, loc, "can't convert to a boolean")
  end
end