Module: Strings

Defined in:
lib/LIVR/Rules/Strings.rb

Class Method Summary collapse

Class Method Details

.length_between(args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/LIVR/Rules/Strings.rb', line 48

def self.length_between(args)
  min_length, max_length = args

  lambda do |value, unuse, unuse_|
    return if value.nil? or value.eql?('')
    return 'FORMAT_ERROR' unless value.kind_of? String
    return 'TOO_SHORT' if value.length < min_length
    return 'TOO_LONG'  if value.length > max_length
  end
end

.length_equal(args) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/LIVR/Rules/Strings.rb', line 38

def self.length_equal(args)
  length = args.shift.to_i
  lambda do |value, unuse, unuse_|
    return if value.nil? or value.eql?('')
    return 'FORMAT_ERROR' unless value.kind_of? String
    return 'TOO_SHORT' if value.length < length
    return 'TOO_LONG'  if value.length > length
  end
end

.like(args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/LIVR/Rules/Strings.rb', line 59

def self.like(args)
  args.pop                # pop rule_builders
  re = args[0]
  is_ignore_case = args.length == 2 && args[1] == 'i'
  re = is_ignore_case ? %r(#{re})i : %r(#{re})

  lambda do |value, unuse, unuse_|
    return if value.nil? or value.eql?('')
    return 'FORMAT_ERROR' unless Util.is_string_or_number?(value)
    return 'WRONG_FORMAT' unless value =~ re
  end
end

.max_length(args) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/LIVR/Rules/Strings.rb', line 19

def self.max_length(args)
  max_length = args.shift.to_i

  lambda do |value, unuse, unuse_|
    return if value.nil? or value.eql?('')
    return 'FORMAT_ERROR' unless value.kind_of? String
    return 'TOO_LONG' if value.length > max_length
  end
end

.min_length(args) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/LIVR/Rules/Strings.rb', line 29

def self.min_length(args)
  min_length = args.shift.to_i
  lambda do |value, unuse, unuse_|
    return if value.nil? or value.eql?('')
    return 'FORMAT_ERROR' unless value.kind_of? String
    return 'TOO_SHORT' if value.length < min_length
  end
end

.one_of(args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/LIVR/Rules/Strings.rb', line 4

def self.one_of(args)
  if args[0].kind_of? Array
    allowed_values = args.shift
  else
    args.pop              # pop rule_builders
    allowed_values = args
  end

  lambda do |value, unuse, unuse_|
    return if value.nil? or value.eql?('')
    return 'FORMAT_ERROR' unless value.kind_of? String
    return 'NOT_ALLOWED_VALUE' unless allowed_values.include? value
  end
end