egn 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/egn/generator.rb +5 -4
- data/lib/egn/version.rb +1 -1
- data/spec/egn/generator_spec.rb +22 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c50e9550220d451fd878ef94ef66a035d040ef2d
|
4
|
+
data.tar.gz: 0c4120ed1258d8e2ce27327dc2b4267d23152106
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48c9632edb348b3f00872c0fb12a41230c031c75bf7788d19f95ac08398e5b2b8555c0cc7e63ffce757d4593c19588a31a15fcbdb179a98b5abe23fcb0c35c67
|
7
|
+
data.tar.gz: 19cb7bbc9e23fba5ba47f6b1489ce1a9722b5e88aed4606f907105cb5392e40c369e16fdb42b5a2d14b85568e7618aef0f019ab1bd5e45bae15c4d56463aa694
|
data/lib/egn/generator.rb
CHANGED
@@ -32,6 +32,7 @@ module Egn
|
|
32
32
|
raise ArgumentError, "Year out of bounds" unless (1800..2099).include?(options[:year])
|
33
33
|
raise ArgumentError, "Month out of bounds" unless (1..12).include?(options[:month])
|
34
34
|
raise ArgumentError, "Day out of bounds" unless (1..31).include?(options[:day])
|
35
|
+
raise ArgumentError, "Invalid sex; valid values: [:male, :female]" unless [:male, :female].include?(options[:sex])
|
35
36
|
end
|
36
37
|
|
37
38
|
def defaults
|
@@ -39,7 +40,8 @@ module Egn
|
|
39
40
|
{
|
40
41
|
year: date.year,
|
41
42
|
month: date.month,
|
42
|
-
day: date.day
|
43
|
+
day: date.day,
|
44
|
+
sex: [:male, :female].sample
|
43
45
|
}
|
44
46
|
end
|
45
47
|
|
@@ -47,16 +49,15 @@ module Egn
|
|
47
49
|
# Get random century, region and sex
|
48
50
|
options[:century] = options[:year] - (options[:year] % 100)
|
49
51
|
options[:region] = Random.rand(0..999)
|
50
|
-
options[:sex] = Random.rand(1..2)
|
51
52
|
|
52
53
|
# Recalculate month based on the century
|
53
54
|
options[:month] += 20 if options[:century] == 1800
|
54
55
|
options[:month] += 40 if options[:century] == 2000
|
55
56
|
|
56
57
|
# Recalculate region based on sex
|
57
|
-
if options[:sex] ==
|
58
|
+
if options[:sex] == :male && options[:region].odd?
|
58
59
|
options[:region] -= 1
|
59
|
-
elsif options[:sex] ==
|
60
|
+
elsif options[:sex] == :female && options[:region].even?
|
60
61
|
options[:region] += 1
|
61
62
|
end
|
62
63
|
|
data/lib/egn/version.rb
CHANGED
data/spec/egn/generator_spec.rb
CHANGED
@@ -22,7 +22,7 @@ describe Egn::Generator do
|
|
22
22
|
|
23
23
|
context "invoked with arguments" do
|
24
24
|
it "generates a new EGN considering the given options" do
|
25
|
-
number = Egn::Generator.generate(year: 1990, month: 12, day: 30, sex: :
|
25
|
+
number = Egn::Generator.generate(year: 1990, month: 12, day: 30, sex: :male)
|
26
26
|
egn = Egn::Egn.new(number)
|
27
27
|
|
28
28
|
expect(egn).to be_valid
|
@@ -49,16 +49,29 @@ describe Egn::Generator do
|
|
49
49
|
expect(egn.day).to eq(15)
|
50
50
|
end
|
51
51
|
|
52
|
+
it "generates female EGNs" do
|
53
|
+
number = Egn::Generator.generate(sex: :female)
|
54
|
+
egn = Egn::Egn.new(number)
|
55
|
+
|
56
|
+
expect(egn.sex).to eq(:female)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "generates male EGNs" do
|
60
|
+
number = Egn::Generator.generate(sex: :male)
|
61
|
+
egn = Egn::Egn.new(number)
|
62
|
+
|
63
|
+
expect(egn.sex).to eq(:male)
|
64
|
+
end
|
65
|
+
|
52
66
|
it "validates the options" do
|
53
67
|
|
54
|
-
options = {year: 1960, month: 6, day: 3}
|
68
|
+
options = {year: 1960, month: 6, day: 3, sex: :male}
|
55
69
|
|
56
70
|
Egn::Generator.any_instance.should_receive(:validate!).with(options)
|
57
71
|
|
58
72
|
Egn::Generator.generate(options)
|
59
73
|
end
|
60
74
|
|
61
|
-
|
62
75
|
end
|
63
76
|
end
|
64
77
|
|
@@ -82,6 +95,12 @@ describe Egn::Generator do
|
|
82
95
|
}.to raise_error ArgumentError
|
83
96
|
end
|
84
97
|
|
98
|
+
it "raises an exception if invalid sex is given" do
|
99
|
+
expect{
|
100
|
+
Egn::Generator.generate(sex: :none)
|
101
|
+
}.to raise_error ArgumentError
|
102
|
+
end
|
103
|
+
|
85
104
|
end
|
86
105
|
|
87
106
|
end
|