Class: CommonNumbers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/common_numbers/base.rb

Direct Known Subclasses

Polish::Nip, Polish::Pesel, Regon14, Regon9

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num) ⇒ Base

Returns a new instance of Base.



7
8
9
# File 'lib/common_numbers/base.rb', line 7

def initialize(num)
  @magick_number = num.to_s.gsub('-', '')
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



5
6
7
# File 'lib/common_numbers/base.rb', line 5

def length
  @length
end

#magick_arrayObject (readonly)

Returns the value of attribute magick_array.



5
6
7
# File 'lib/common_numbers/base.rb', line 5

def magick_array
  @magick_array
end

#magick_numberObject (readonly)

Returns the value of attribute magick_number.



5
6
7
# File 'lib/common_numbers/base.rb', line 5

def magick_number
  @magick_number
end

#maskObject (readonly)

Returns the value of attribute mask.



5
6
7
# File 'lib/common_numbers/base.rb', line 5

def mask
  @mask
end

#moduloObject (readonly)

Returns the value of attribute modulo.



5
6
7
# File 'lib/common_numbers/base.rb', line 5

def modulo
  @modulo
end

#regexpObject (readonly)

Returns the value of attribute regexp.



5
6
7
# File 'lib/common_numbers/base.rb', line 5

def regexp
  @regexp
end

Instance Method Details

#checksumObject



33
34
35
36
# File 'lib/common_numbers/base.rb', line 33

def checksum
  @magick_array = magick_number.split( "").collect &:to_i
  checksum = mask.inject(0) {|sum, weight| sum + weight * magick_array.shift}
end

#valid?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/common_numbers/base.rb', line 11

def valid?
  validate
end

#validateObject



15
16
17
# File 'lib/common_numbers/base.rb', line 15

def validate
  validate_regexp && validate_length && validate_sum_control
end

#validate_lengthObject



23
24
25
# File 'lib/common_numbers/base.rb', line 23

def validate_length
  magick_number.size == length
end

#validate_regexpObject



19
20
21
# File 'lib/common_numbers/base.rb', line 19

def validate_regexp
    regexp =~ magick_number
end

#validate_sum_controlObject



27
28
29
30
31
# File 'lib/common_numbers/base.rb', line 27

def validate_sum_control
  mod = checksum % modulo
  mod = 0 if mod == 10
  mod === magick_array.shift
end