Class: ABN
- Inherits:
-
Object
- Object
- ABN
- Defined in:
- lib/abn.rb
Defined Under Namespace
Modules: Version
Class Method Summary collapse
-
.valid?(abn) ⇒ Boolean
Accepts an ABN number as a String or Bignum and returns whether or not it is valid (not checked against a database).
Instance Method Summary collapse
-
#initialize(num) ⇒ ABN
constructor
Creates an ABN object representing the ABN number passed as the only parameter.
-
#to_s ⇒ Object
Correctly formats the represented ABN if valid, else returns an empty string.
-
#valid? ⇒ Boolean
Returns whether the current ABN class represents a valid ABN number according to a weighting algorithm (not checked against a datbase).
Constructor Details
#initialize(num) ⇒ ABN
Creates an ABN object representing the ABN number passed as the only parameter.
13 14 15 |
# File 'lib/abn.rb', line 13 def initialize(num) @number = num.to_s.tr ' ','' end |
Class Method Details
.valid?(abn) ⇒ Boolean
Accepts an ABN number as a String or Bignum and returns whether or not it is valid (not checked against a database)
42 43 44 |
# File 'lib/abn.rb', line 42 def self.valid?(abn) new(abn).valid? end |
Instance Method Details
#to_s ⇒ Object
Correctly formats the represented ABN if valid, else returns an empty string
36 37 38 |
# File 'lib/abn.rb', line 36 def to_s valid? ? "%s%s %s%s%s %s%s%s %s%s%s" % @number.split('') : "" end |
#valid? ⇒ Boolean
Returns whether the current ABN class represents a valid ABN number according to a weighting algorithm (not checked against a datbase)
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/abn.rb', line 20 def valid? return false unless @number.length == 11 weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19] sum = 0 (0..10).each do |i| c = @number[i,1] digit = c.to_i - (i.zero? ? 1 : 0) sum += weights[i] * digit end sum % 89 == 0 ? true : false end |