Class: Ssn::SocialSecurityNumber

Inherits:
Object
  • Object
show all
Defined in:
lib/ssn/social_security_number.rb

Constant Summary collapse

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})$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ SocialSecurityNumber

Returns a new instance of SocialSecurityNumber.



11
12
13
14
15
# File 'lib/ssn/social_security_number.rb', line 11

def initialize( value=nil )
  return if value.nil? || value.empty?
  return if value == '000000000' || value == '000-00-0000'
  @raw = SocialSecurityNumber.parse( value )
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



9
10
11
# File 'lib/ssn/social_security_number.rb', line 9

def raw
  @raw
end

Class Method Details

.format(value) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/ssn/social_security_number.rb', line 27

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

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

.parse(value) ⇒ Object



21
22
23
24
25
# File 'lib/ssn/social_security_number.rb', line 21

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

Instance Method Details

#formattedObject



17
18
19
# File 'lib/ssn/social_security_number.rb', line 17

def formatted
  SocialSecurityNumber.format raw
end