Module: InputValidator

Overview

Simple helper module for input validation

Constant Summary collapse

URI_RE =
/\A#{URI.regexp}\z/
NUMBER_RE =
/\A[[:digit:]]+\z/
DECIMAL_RE =
/\A[[:digit:]]+\.[[:digit:]]+\z/
STRING_RE =
/\A[[:print:]]+\z/
NON_ZERO_NUMBER_RE =
/\A[1-9][[:digit:]]*\z/

Instance Method Summary collapse

Instance Method Details

#decimal?(object) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/input_validator.rb', line 19

def decimal?(object)
  is?(object, DECIMAL_RE) || number?(object)
end

#is?(object, regexp) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/input_validator.rb', line 11

def is?(object, regexp)
  object.to_s =~ regexp
end

#non_zero_number?(object) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/input_validator.rb', line 31

def non_zero_number?(object)
  is?(object, NON_ZERO_NUMBER_RE)
end

#number?(object) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/input_validator.rb', line 15

def number?(object)
  is?(object, NUMBER_RE)
end

#string?(object) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/input_validator.rb', line 27

def string?(object)
  is?(object, STRING_RE)
end

#uri?(object) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/input_validator.rb', line 23

def uri?(object)
  is?(object, URI_RE)
end