egn 0.4.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/egn/generator.rb CHANGED
@@ -1,49 +1,68 @@
1
1
  # Generates a random valid EGN
2
2
  module Egn
3
- module Generator
3
+ class Generator
4
+ attr_reader :options
4
5
 
5
- # The generated EGN will be completely random if no opitons are given.
6
- # options is a hash that may have the following keys: :year, :month and :date
6
+ # Convinience method
7
7
  def self.generate(options={})
8
- date = Util.time_rand
9
-
10
- options = {
11
- year: date.year,
12
- month: date.month,
13
- day: date.day
14
- }.merge(options)
15
-
16
- validate!(options)
17
-
18
- century = options[:year] - (options[:year] % 100)
19
- sex = Random.rand(1..2)
8
+ Generator.new(options).generate
9
+ end
20
10
 
21
- if century == 1800
22
- options[:month] += 20
23
- elsif century == 2000
24
- options[:month] += 40
25
- end
11
+ def initialize(options={})
12
+ @options = defaults.merge(options)
26
13
 
27
- region = Random.rand(0..999)
14
+ validate!(@options)
15
+ end
28
16
 
29
- if sex == 1 && (region %2 != 0)
30
- region -= 1
31
- elsif sex == 2 && (region % 2 == 0)
32
- region += 1
33
- end
17
+ # The generated EGN will be completely random if no opitons are given.
18
+ # options is a hash that may have the following keys: :year, :month and :date
19
+ def generate
20
+ randomize_options
34
21
 
35
- final_year = options[:year] - century
36
- egn = final_year.to_s.rjust(2, '0') + options[:month].to_s.rjust(2, '0') + options[:day].to_s.rjust(2,'0') + region.to_s.rjust(3,'0')
22
+ egn = options[:year].to_s.rjust(2, '0') +
23
+ options[:month].to_s.rjust(2, '0') +
24
+ options[:day].to_s.rjust(2,'0') +
25
+ options[:region].to_s.rjust(3,'0')
37
26
 
38
27
  return egn + Util.egn_checksum(egn).to_s
39
28
  end
40
29
 
41
30
  # Check if the options contain a date that is valid and be turned into an EGN
42
- def self.validate!(options)
31
+ def validate!(options)
43
32
  raise ArgumentError, "Year out of bounds" unless (1800..2099).include?(options[:year])
44
33
  raise ArgumentError, "Month out of bounds" unless (1..12).include?(options[:month])
45
34
  raise ArgumentError, "Day out of bounds" unless (1..31).include?(options[:day])
46
35
  end
47
36
 
37
+ def defaults
38
+ date = Util.time_rand
39
+ {
40
+ year: date.year,
41
+ month: date.month,
42
+ day: date.day
43
+ }
44
+ end
45
+
46
+ def randomize_options
47
+ # Get random century, region and sex
48
+ options[:century] = options[:year] - (options[:year] % 100)
49
+ options[:region] = Random.rand(0..999)
50
+ options[:sex] = Random.rand(1..2)
51
+
52
+ # Recalculate month based on the century
53
+ options[:month] += 20 if options[:century] == 1800
54
+ options[:month] += 40 if options[:century] == 2000
55
+
56
+ # Recalculate region based on sex
57
+ if options[:sex] == 1 && options[:region].odd?
58
+ options[:region] -= 1
59
+ elsif options[:sex] == 2 && options[:region].even?
60
+ options[:region] += 1
61
+ end
62
+
63
+ options[:year] = options[:year] - options[:century]
64
+ end
65
+
66
+
48
67
  end
49
68
  end
data/lib/egn/validator.rb CHANGED
@@ -1,8 +1,18 @@
1
1
  module Egn
2
2
  class Validator
3
+ attr_reader :egn
3
4
 
4
- # Checks if a given EGN is valid
5
+ # Convinience method
5
6
  def self.validate(egn)
7
+ Validator.new(egn).validate
8
+ end
9
+
10
+ def initialize(egn)
11
+ @egn = egn
12
+ end
13
+
14
+ # Checks if a given EGN is valid
15
+ def validate
6
16
  return false unless egn.length == 10
7
17
 
8
18
  # Extract the correct year and month
data/lib/egn/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Egn
2
- VERSION = "0.4.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -53,7 +53,7 @@ describe Egn::Generator do
53
53
 
54
54
  options = {year: 1960, month: 6, day: 3}
55
55
 
56
- Egn::Generator.should_receive(:validate!).with(options)
56
+ Egn::Generator.any_instance.should_receive(:validate!).with(options)
57
57
 
58
58
  Egn::Generator.generate(options)
59
59
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: egn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-26 00:00:00.000000000 Z
12
+ date: 2014-04-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -117,7 +117,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
117
  version: '0'
118
118
  segments:
119
119
  - 0
120
- hash: 152494033
120
+ hash: 3961938268472800387
121
121
  required_rubygems_version: !ruby/object:Gem::Requirement
122
122
  none: false
123
123
  requirements:
@@ -126,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
126
  version: '0'
127
127
  segments:
128
128
  - 0
129
- hash: 152494033
129
+ hash: 3961938268472800387
130
130
  requirements: []
131
131
  rubyforge_project:
132
132
  rubygems_version: 1.8.23