Class: CommonNumbers::Base
- Inherits:
-
Object
- Object
- CommonNumbers::Base
- Defined in:
- lib/common_numbers/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#magick_array ⇒ Object
readonly
Returns the value of attribute magick_array.
-
#magick_number ⇒ Object
readonly
Returns the value of attribute magick_number.
-
#mask ⇒ Object
readonly
Returns the value of attribute mask.
-
#modulo ⇒ Object
readonly
Returns the value of attribute modulo.
-
#regexp ⇒ Object
readonly
Returns the value of attribute regexp.
Instance Method Summary collapse
- #checksum ⇒ Object
-
#initialize(num) ⇒ Base
constructor
A new instance of Base.
- #valid? ⇒ Boolean
- #validate ⇒ Object
- #validate_length ⇒ Object
- #validate_regexp ⇒ Object
- #validate_sum_control ⇒ Object
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
#length ⇒ Object (readonly)
Returns the value of attribute length.
5 6 7 |
# File 'lib/common_numbers/base.rb', line 5 def length @length end |
#magick_array ⇒ Object (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_number ⇒ Object (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 |
#mask ⇒ Object (readonly)
Returns the value of attribute mask.
5 6 7 |
# File 'lib/common_numbers/base.rb', line 5 def mask @mask end |
#modulo ⇒ Object (readonly)
Returns the value of attribute modulo.
5 6 7 |
# File 'lib/common_numbers/base.rb', line 5 def modulo @modulo end |
#regexp ⇒ Object (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
#checksum ⇒ Object
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
11 12 13 |
# File 'lib/common_numbers/base.rb', line 11 def valid? validate end |
#validate ⇒ Object
15 16 17 |
# File 'lib/common_numbers/base.rb', line 15 def validate validate_regexp && validate_length && validate_sum_control end |
#validate_length ⇒ Object
23 24 25 |
# File 'lib/common_numbers/base.rb', line 23 def validate_length magick_number.size == length end |
#validate_regexp ⇒ Object
19 20 21 |
# File 'lib/common_numbers/base.rb', line 19 def validate_regexp regexp =~ magick_number end |
#validate_sum_control ⇒ Object
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 |