Class: IdentityCode::LV
- Inherits:
-
Object
- Object
- IdentityCode::LV
- Defined in:
- lib/identity_code/lv.rb
Class Method Summary collapse
Instance Method Summary collapse
- #age ⇒ Object
- #birth_date ⇒ Object
-
#initialize(code) ⇒ LV
constructor
A new instance of LV.
- #sex ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(code) ⇒ LV
Returns a new instance of LV.
5 6 7 |
# File 'lib/identity_code/lv.rb', line 5 def initialize(code) @code = code.to_s.gsub('-', '') end |
Class Method Details
.control_digit(base) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/identity_code/lv.rb', line 38 def self.control_digit(base) array = base.gsub('-', '').split('').map(&:to_i) multipliers = [1, 6, 3, 7, 9, 10, 5, 8, 4, 2] hash = Hash[multipliers.zip(array)] check = 0 hash.map do |k, v| check += k * v end ((1 - check) % 11) % 10 end |
.generate(opts = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/identity_code/lv.rb', line 9 def self.generate(opts = {}) year = opts[:year] || rand(Date.today.year - 90..Date.today.year - 1) year = rand(Date.today.year - 50..Date.today.year - 21) if opts[:safe_age] month = opts[:month] || rand(1..12) day = opts[:day] || rand(1..NUM_DAYS[month]) century_code = begin case year when 1800..1899 then 0 when 1900..1999 then 1 when 2000..2099 then 2 else 9 end end.to_s result = "%02d" % day result += "%02d" % month result += "%02d" % year.to_s[2..3].to_i result += '-' if opts[:separator] result += century_code.to_s result += "%03d" % rand(1..999).to_s result += control_digit(result).to_s end |
.valid?(code) ⇒ Boolean
34 35 36 |
# File 'lib/identity_code/lv.rb', line 34 def self.valid?(code) new(code).valid? end |
Instance Method Details
#age ⇒ Object
65 66 67 68 69 |
# File 'lib/identity_code/lv.rb', line 65 def age return unless valid? now = Time.now.utc.to_date now.year - (birth_date.year + IdentityCode.age_correction(birth_date)) end |
#birth_date ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/identity_code/lv.rb', line 56 def birth_date return unless valid? year = century + @code[4..5].to_i month = @code[2..3].to_i day = @code[0..1].to_i return unless Date.valid_date?(year, month, day) Date.new(year, month, day) end |
#sex ⇒ Object
71 72 73 |
# File 'lib/identity_code/lv.rb', line 71 def sex nil end |
#valid? ⇒ Boolean
51 52 53 54 |
# File 'lib/identity_code/lv.rb', line 51 def valid? @code.length == 11 && @code[10].chr.to_i == self.class.control_digit(@code) end |