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

- old
+ new

@@ -1,5 +1,28 @@ 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? + @raw = SocialSecurityNumber.parse( value ) + end + + def formatted + return (raw.nil? || raw.empty?) ? + '' : + SocialSecurityNumber.format( raw ) + end + + def self.parse( value ) + value.gsub( /-/, "" ) + end + + def self.format( value ) + parse( value ).gsub( UNFORMATTED_CAPTURE_REGEX,"\\1-\\2-\\3" ) + end end end \ No newline at end of file