ssn 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,6 +11,7 @@ Encapsulates functionality for storing an unformatted SSN but formatting the SSN
11
11
  == FEATURES
12
12
 
13
13
  * Store a SSN unformatted in a database field with auto formatting when using.
14
+ * SocialSecurityNumber class to use for formatting SSNs.
14
15
 
15
16
 
16
17
  == REQUIREMENTS
@@ -47,6 +48,8 @@ Run:
47
48
 
48
49
  == USAGE
49
50
 
51
+ === Active Record Extension
52
+
50
53
  Add a raw_ssn that is at least 9 in length to the database table of the model you desire to track a SSN on.
51
54
 
52
55
  Specify usage in your model:
@@ -64,6 +67,15 @@ Use the field:
64
67
  person.ssn # => 123-45-6789
65
68
 
66
69
 
70
+ === SocialSecurityNumber Class
71
+
72
+ ssn = Ssn::SocialSecurityNumber.new( '123456789' )
73
+
74
+ ssn.raw # => 123456789
75
+ ssn.formatted # => 123-45-6789
76
+
77
+
78
+
67
79
  == Note on Patches/Pull Requests
68
80
 
69
81
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -12,9 +12,9 @@ module Ssn
12
12
  self.class_eval { extend ClassMethods }
13
13
  include InstanceMethods
14
14
  validates_length_of :"raw_#{args.first.to_sym}", :maximum => 9, :allow_blank => true
15
- validates_format_of :"raw_#{args.first.to_sym}", :with => /^[0-9]{9}$/, :allow_blank => true
15
+ validates_format_of :"raw_#{args.first.to_sym}", :with => Ssn::SocialSecurityNumber::UNFORMATTED_REGEX, :allow_blank => true
16
16
 
17
- validates_format_of args.first.to_sym, :with => /^[0-9]{3}-?[0-9]{2}-?[0-9]{4}$/, :allow_blank => true
17
+ validates_format_of args.first.to_sym, :with => Ssn::SocialSecurityNumber::FORMATTED_REGEX, :allow_blank => true
18
18
  end
19
19
 
20
20
  initialize_has_ssn_from_args args
@@ -38,7 +38,7 @@ module Ssn
38
38
 
39
39
  def initialize_has_ssn_from_string( str )
40
40
  define_method str do
41
- return raw_ssn.blank? ? nil : raw_ssn.gsub( /^([0-9]{3})([0-9]{2})([0-9]{4})$/,"\\1-\\2-\\3" )
41
+ return raw_ssn.blank? ? nil : raw_ssn.gsub( Ssn::SocialSecurityNumber::UNFORMATTED_CAPTURE_REGEX, "\\1-\\2-\\3" )
42
42
  end
43
43
 
44
44
  define_method "#{str}=" do |value|
@@ -1,5 +1,28 @@
1
1
  module Ssn
2
2
  class SocialSecurityNumber
3
+ FORMATTED_REGEX = /^[0-9]{3}-?[0-9]{2}-?[0-9]{4}$/
4
+ UNFORMATTED_REGEX = /^[0-9]{9}$/
5
+ UNFORMATTED_CAPTURE_REGEX = /^([0-9]{3})([0-9]{2})([0-9]{4})$/
3
6
 
7
+ attr_reader :raw
8
+
9
+ def initialize( value=nil )
10
+ return if value.nil?
11
+ @raw = SocialSecurityNumber.parse( value )
12
+ end
13
+
14
+ def formatted
15
+ return (raw.nil? || raw.empty?) ?
16
+ '' :
17
+ SocialSecurityNumber.format( raw )
18
+ end
19
+
20
+ def self.parse( value )
21
+ value.gsub( /-/, "" )
22
+ end
23
+
24
+ def self.format( value )
25
+ parse( value ).gsub( UNFORMATTED_CAPTURE_REGEX,"\\1-\\2-\\3" )
26
+ end
4
27
  end
5
28
  end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Ssn::SocialSecurityNumber" do
4
+ before :each do
5
+ @klass = Ssn::SocialSecurityNumber
6
+ @instance = @klass.new
7
+ end
8
+
9
+ it "should agree that the UNFORMATTED_REGEX is /^[0-9]{9}$/" do
10
+ @klass::UNFORMATTED_REGEX.should == /^[0-9]{9}$/
11
+ end
12
+
13
+ it "should agree that the FORMATTED_REGEX is /^[0-9]{3}-?[0-9]{2}-?[0-9]{4}$/" do
14
+ @klass::FORMATTED_REGEX.should == /^[0-9]{3}-?[0-9]{2}-?[0-9]{4}$/
15
+ end
16
+
17
+ it "should agree that the UNFORMATTED_CAPTURE_REGEX is /^([0-9]{3})([0-9]{2})([0-9]{4})$/" do
18
+ @klass::UNFORMATTED_CAPTURE_REGEX.should == /^([0-9]{3})([0-9]{2})([0-9]{4})$/
19
+ end
20
+
21
+ it "should initialize when given an unformatted SSN" do
22
+ @klass.new( '123456789' ).raw.should == '123456789'
23
+ end
24
+
25
+ it "should initialize when given an unformatted SSN" do
26
+ @klass.new( '123-45-6789' ).raw.should == '123456789'
27
+ end
28
+
29
+ it "should return a formatted SSN" do
30
+ @klass.new( '123456789' ).formatted.should == '123-45-6789'
31
+ end
32
+
33
+ it "should return a '' when a the SSN is blank" do
34
+ @klass.new( '' ).formatted.should == ''
35
+ end
36
+
37
+ it "should return a '' when a the SSN is nil" do
38
+ @klass.new.formatted.should == ''
39
+ end
40
+
41
+ context "well formed SSNs" do
42
+ it "should parse an SSN without formatting" do
43
+ @klass.parse( '123456789' ).should == '123456789'
44
+ end
45
+
46
+ it "should parse an SSN with formatting" do
47
+ @klass.parse( '123-45-6789' ).should == '123456789'
48
+ end
49
+
50
+ it "should format an SSN without formatting" do
51
+ @klass.format( '123456789' ).should == '123-45-6789'
52
+ end
53
+
54
+ it "should format an SSN with formatting" do
55
+ @klass.format( '123-45-6789' ).should == '123-45-6789'
56
+ end
57
+ end
58
+ end
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  describe "Ssn" do
4
4
  describe "having ActiveRecord extensions" do
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ssn}
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jason Harrelson"]
12
- s.date = %q{2010-11-05}
12
+ s.date = %q{2010-11-10}
13
13
  s.description = %q{Encapsulates functionality for storing an unformatted SSN but formatting the SSN on print.}
14
14
  s.email = %q{jason@lookforwardenterprises.com}
15
15
  s.extra_rdoc_files = [
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
32
32
  "spec/database.yml",
33
33
  "spec/spec.opts",
34
34
  "spec/spec_helper.rb",
35
+ "spec/ssn/social_security_number_spec.rb",
35
36
  "spec/ssn_spec.rb",
36
37
  "ssn.gemspec"
37
38
  ]
@@ -42,6 +43,7 @@ Gem::Specification.new do |s|
42
43
  s.summary = %q{Encapsulates functionality for storing an unformatted SSN but formatting the SSN on print.}
43
44
  s.test_files = [
44
45
  "spec/spec_helper.rb",
46
+ "spec/ssn/social_security_number_spec.rb",
45
47
  "spec/ssn_spec.rb"
46
48
  ]
47
49
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ssn
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jason Harrelson
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-05 00:00:00 -05:00
18
+ date: 2010-11-10 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -59,6 +59,7 @@ files:
59
59
  - spec/database.yml
60
60
  - spec/spec.opts
61
61
  - spec/spec_helper.rb
62
+ - spec/ssn/social_security_number_spec.rb
62
63
  - spec/ssn_spec.rb
63
64
  - ssn.gemspec
64
65
  has_rdoc: true
@@ -97,4 +98,5 @@ specification_version: 3
97
98
  summary: Encapsulates functionality for storing an unformatted SSN but formatting the SSN on print.
98
99
  test_files:
99
100
  - spec/spec_helper.rb
101
+ - spec/ssn/social_security_number_spec.rb
100
102
  - spec/ssn_spec.rb