lib/ssn/social_security_number.rb in ssn-0.3.0 vs lib/ssn/social_security_number.rb in ssn-1.0.0

- old
+ new

@@ -1,28 +1,37 @@ module Ssn + class SocialSecurityNumber + FORMATTED_REGEX = /^[0-9]{3}-?[0-9]{2}-?[0-9]{4}$/ UNFORMATTED_REGEX = /^[0-9]{9}$/ UNFORMATTED_CAPTURE_REGEX = /^([0-9]{3})([0-9]{2})([0-9]{4})$/ attr_reader :raw def initialize( value=nil ) - return if value.nil? + return if value.nil? || value.empty? + return if value == '000000000' || value == '000-00-0000' @raw = SocialSecurityNumber.parse( value ) end def formatted - return (raw.nil? || raw.empty?) ? - '' : - SocialSecurityNumber.format( raw ) + SocialSecurityNumber.format raw end def self.parse( value ) - value.gsub( /-/, "" ) + value.nil? ? + nil : + value.gsub( /-/, "" ) end def self.format( value ) - parse( value ).gsub( UNFORMATTED_CAPTURE_REGEX,"\\1-\\2-\\3" ) + parsed = parse( value ) + + parsed.nil? ? + nil : + parsed.gsub( UNFORMATTED_CAPTURE_REGEX,"\\1-\\2-\\3" ) end + end -end \ No newline at end of file + +end