luhn 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: acc7be20238719d56f27cf60436232d6933aeca2
4
- data.tar.gz: 351bc940bea52556e369602a546c5bcebdaf1e02
3
+ metadata.gz: eece57290c12c551a1673bac68bbaf0b9674c5b2
4
+ data.tar.gz: e86edca6abea3b65d71edddef8114d65e1b2015c
5
5
  SHA512:
6
- metadata.gz: 1c25701d9c3466526efc767fee9c0c0c7c384044c8894d84e635094b687aba5be016a1b99cfda8f0c7181693c7ac89a8cdd2d664263517d2b7132d0e1614c588
7
- data.tar.gz: 6e0cff66c776898bb10ef90cb58129af34e64dfbef5b569af2f7998a8155c755c30d5ced4fd21f538f6f03d85e5cbb3930ea046b8752982e46723743a6cfae68
6
+ metadata.gz: 9149f14748ef4a961500f622dd114702f8c100c0425f75bddd0f1fae842a443139102598417aef83fc3d0d0dd9cec4d0bb0e26d037e6555e278d82ad8489cec3
7
+ data.tar.gz: 03dd2de39b1698ec77d947db29dc297cf4eb4d2ceec23e31170cd72047f67196b8da5e7f09f2ee48ba89a0b68cf92206272c4bd8f070d550b1a0b16d6efb4917
@@ -2,14 +2,14 @@
2
2
  require 'ostruct'
3
3
  module Luhn
4
4
  class CivicNumber
5
- attr_reader :civic_number
5
+ attr_reader :value
6
6
 
7
- def initialize(string)
8
- @civic_number = cleanup_string(string.to_s)
7
+ def initialize string
8
+ self.value = string
9
9
  end
10
10
 
11
11
  def valid?
12
- @valid ||= civic_number.length == 10 && valid_date? && Luhn.valid?(civic_number)
12
+ @valid ||= value.length == 10 && valid_date? && Luhn.valid?(value)
13
13
  end
14
14
 
15
15
  def valid_date?
@@ -17,11 +17,11 @@ module Luhn
17
17
  end
18
18
 
19
19
  def control_digit
20
- @control_digit ||= Luhn.control_digit(civic_number[0...9])
20
+ @control_digit ||= Luhn.control_digit(value[0...9])
21
21
  end
22
22
 
23
23
  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'
25
25
  end
26
26
 
27
27
  def female?
@@ -33,34 +33,38 @@ module Luhn
33
33
  end
34
34
 
35
35
  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
40
40
  })
41
41
  end
42
42
 
43
43
  def formatted
44
- to_s.insert(civic_number.length - 4, "-")
44
+ return value if value.length < 10
45
+
46
+ value.insert(value.length - 4, "-")
45
47
  end
46
48
 
47
49
  def to_s
48
- civic_number
50
+ value
49
51
  end
50
52
 
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
56
56
  end
57
57
 
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"))
63
61
  end
64
62
 
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
65
69
  end
66
70
  end
@@ -1,3 +1,3 @@
1
1
  module Luhn
2
- Version = "1.0.0"
2
+ Version = "1.0.2"
3
3
  end
@@ -46,10 +46,18 @@ describe Luhn::CivicNumber do
46
46
  civic_number.birth_date.day.must_equal 1
47
47
  end
48
48
 
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')
51
58
 
52
- civic_number.formatted.must_equal "300101-8194"
59
+ civic_number.formatted.must_equal "300101819"
60
+ end
53
61
  end
54
62
 
55
63
  it 'generates a valid random civic number' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: luhn
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Junström