Module: TerminalHelpers::Validations
Constant Summary collapse
- FORMATS =
{ :domain => /^[a-z\d-]+(\.[a-z\d-]+)*\.(([\d]{1,3})|([a-z]{2,3})|(aero|coop|info|museum|name))$/i, :email => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :zipcode => /^[\d]{5}$/, :ipv4 => /^[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}$/ }
Instance Method Summary collapse
- #domain(value) ⇒ Object
- #email(value) ⇒ Object
- #ipv4(value) ⇒ Object
-
#validate(value, format, raise_error = false) ⇒ Object
Validate data against provided format.
- #zipcode(value) ⇒ Object
Instance Method Details
#domain(value) ⇒ Object
26 |
# File 'lib/terminal_helpers/validations.rb', line 26 def domain(value) ; validate(value, :domain) ; end |
#email(value) ⇒ Object
25 |
# File 'lib/terminal_helpers/validations.rb', line 25 def email(value) ; validate(value, :email) ; end |
#ipv4(value) ⇒ Object
28 |
# File 'lib/terminal_helpers/validations.rb', line 28 def ipv4(value) ; validate(value, :ipv4) ; end |
#validate(value, format, raise_error = false) ⇒ Object
Validate data against provided format
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/terminal_helpers/validations.rb', line 14 def validate(value, format, raise_error=false) unless FORMATS.key?(format) raise FormatError, "Invalid data format: #{format}" end result = value =~ FORMATS[format] ? true : false if raise_error && !result raise ValidationError, "Invalid value \"#{value}\" for #{format}" end result end |
#zipcode(value) ⇒ Object
27 |
# File 'lib/terminal_helpers/validations.rb', line 27 def zipcode(value) ; validate(value, :zipcode) ; end |