Sha256: 24b720ebdef7af35427d9959afadb097fed607c49bd0a60987bf42e262fe59e5

Contents?: true

Size: 780 Bytes

Versions: 3

Compression:

Stored size: 780 Bytes

Contents

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? || value.empty?
      return if value == '000000000' || value == '000-00-0000'
      @raw = SocialSecurityNumber.parse( value )
    end

    def formatted
      SocialSecurityNumber.format raw
    end

    def self.parse( value )
      value.nil? ?
        nil :
        value.gsub( /-/, "" )
    end

    def self.format( value )
      parsed = parse( value )

      parsed.nil? ?
        nil :
        parsed.gsub( UNFORMATTED_CAPTURE_REGEX,"\\1-\\2-\\3" )
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ssn-1.0.2 lib/ssn/social_security_number.rb
ssn-1.0.1 lib/ssn/social_security_number.rb
ssn-1.0.0 lib/ssn/social_security_number.rb