Class: ABN

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

Defined Under Namespace

Modules: Version

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num) ⇒ ABN

Creates an ABN object representing the ABN number passed as the only parameter.



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

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)

Returns:

  • (Boolean)


44
45
46
# File 'lib/abn.rb', line 44

def self.valid?(abn)
  new(abn).valid?
end

Instance Method Details

#to_sObject

Correctly formats the represented ABN if valid, else returns an empty string



38
39
40
# File 'lib/abn.rb', line 38

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)

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/abn.rb', line 22

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