Skip to content

Commit 6ea67ec

Browse files
author
Joel Junström
committed
Do not format to short civic numbers
1 parent a47e7f7 commit 6ea67ec

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

lib/luhn/civic_number.rb

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22
require 'ostruct'
33
module Luhn
44
class CivicNumber
5-
attr_reader :civic_number
5+
attr_reader :value
66

7-
def initialize(string)
8-
@civic_number = cleanup_string(string.to_s)
7+
def initialize string
8+
self.value = string
99
end
1010

1111
def valid?
12-
@valid ||= civic_number.length == 10 && valid_date? && Luhn.valid?(civic_number)
12+
@valid ||= value.length == 10 && valid_date? && Luhn.valid?(value)
1313
end
1414

1515
def valid_date?
1616
(1..12).include?(birth_date.month) && (1..31).include?(birth_date.day)
1717
end
1818

1919
def control_digit
20-
@control_digit ||= Luhn.control_digit(civic_number[0...9])
20+
@control_digit ||= Luhn.control_digit(value[0...9])
2121
end
2222

2323
def sex
24-
@sex ||= valid? ? (civic_number[8...9].to_i.even? ? 'female' : 'male') : 'unknown'
24+
valid? ? (value[8...9].to_i.even? ? 'female' : 'male') : 'unknown'
2525
end
2626

2727
def female?
@@ -33,34 +33,38 @@ def male?
3333
end
3434

3535
def birth_date
36-
@date ||= OpenStruct.new({
37-
:year => civic_number[0...2].to_i,
38-
:month => civic_number[2...4].to_i,
39-
:day => civic_number[4...6].to_i
36+
OpenStruct.new({
37+
:year => value[0...2].to_i,
38+
:month => value[2...4].to_i,
39+
:day => value[4...6].to_i
4040
})
4141
end
4242

4343
def formatted
44-
to_s.insert(civic_number.length - 4, "-")
44+
return value if value.length < 10
45+
46+
value.insert(value.length - 4, "-")
4547
end
4648

4749
def to_s
48-
civic_number
50+
value
4951
end
5052

51-
class << self
52-
def generate
53-
date = Time.local(Time.now.year - rand(100) - 1, rand(12) + 1, rand(31) + 1)
54-
Luhn.generate(10, :prefix => date.strftime("%y%m%d"))
55-
end
53+
# For backwards compability
54+
def civic_number
55+
value
5656
end
5757

58-
private
59-
60-
def cleanup_string(string)
61-
string.gsub!(/\D/, '')
62-
string.length == 12 ? string[2...12] : string
58+
def self.generate
59+
date = Time.local(Time.now.year - rand(100) - 1, rand(12) + 1, rand(31) + 1)
60+
Luhn.generate(10, :prefix => date.strftime("%y%m%d"))
6361
end
6462

63+
private
64+
65+
def value= string
66+
val = string.to_s.gsub(/\D/, '')
67+
@value = val.length == 12 ? val[2...12] : val
68+
end
6569
end
6670
end

spec/spec_civic_number.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,18 @@
4646
civic_number.birth_date.day.must_equal 1
4747
end
4848

49-
it "formats the civic number" do
50-
civic_number = Luhn::CivicNumber.new('3001018194')
49+
describe "#formatted" do
50+
it "formats the civic number" do
51+
civic_number = Luhn::CivicNumber.new('3001018194')
52+
53+
civic_number.formatted.must_equal "300101-8194"
54+
end
55+
56+
it "returns the origial if the civic number is to short" do
57+
civic_number = Luhn::CivicNumber.new('300101819')
5158

52-
civic_number.formatted.must_equal "300101-8194"
59+
civic_number.formatted.must_equal "300101819"
60+
end
5361
end
5462

5563
it 'generates a valid random civic number' do

0 commit comments

Comments
 (0)