Class: RiderServer::Validate::String

Inherits:
Base show all
Defined in:
lib/rider_server/validate/string.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/string.rb', line 15

def default(value)
  @default = value
  self
end

#emptyObject



20
21
22
23
24
25
# File 'lib/rider_server/validate/string.rb', line 20

def empty
  add_predicate do |data|
    [data.empty?, "must be empty"]
  end
  self
end

#not_emptyObject



27
28
29
30
31
32
# File 'lib/rider_server/validate/string.rb', line 27

def not_empty
  add_predicate do |data|
    [!data.empty?, "must not be empty"]
  end
  self
end

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rider_server/validate/string.rb', line 34

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_s
  rescue
    fail(data, loc, "can't convert to a string")
  end

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

  data
end