Class: TinValid::SwedenTin

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/tin_valid/sweden_tin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tin:, birth_date: nil) ⇒ SwedenTin

Returns a new instance of SwedenTin.



7
8
9
10
# File 'lib/tin_valid/sweden_tin.rb', line 7

def initialize(tin:, birth_date: nil)
  @tin = tin
  @birth_date = birth_date
end

Instance Attribute Details

#birth_dateObject (readonly)

Returns the value of attribute birth_date.



12
13
14
# File 'lib/tin_valid/sweden_tin.rb', line 12

def birth_date
  @birth_date
end

#tinObject (readonly)

Returns the value of attribute tin.



12
13
14
# File 'lib/tin_valid/sweden_tin.rb', line 12

def tin
  @tin
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tin_valid/sweden_tin.rb', line 14

def valid?
  VERSIONS.any? do |matcher|
    match = matcher.match(tin)
    next false unless match

    year = match[:year]
    month = match[:month]
    day = match[:day]
    next false unless date_valid?(year, month, day)

    id = match[:id]
    match[:check].to_i == checksum("#{year[..2]}#{month}#{day}#{id}")
  end
end