Class: LuhnBase

Inherits:
Object
  • Object
show all
Defined in:
lib/brid/algorithms/luhn.rb

Direct Known Subclasses

Mod10, Mod11

Instance Method Summary collapse

Constructor Details

#initialize(number, options = {}) ⇒ LuhnBase

Returns a new instance of LuhnBase.



2
3
4
5
6
7
# File 'lib/brid/algorithms/luhn.rb', line 2

def initialize number, options = {}
  @mod, @base = 0, 0..100
  @number     = number.to_s.scan(/\d/).join
  @base       = options[:base]  if options.has_key? :base
  @mod        = options[:mod]   if options.has_key? :mod
end

Instance Method Details

#*(digits) ⇒ Object



13
14
15
16
17
# File 'lib/brid/algorithms/luhn.rb', line 13

def * digits
  digits.times.reduce(self) do |number, digit|
    number += self.class.new(number, :base => @base, :mode => @mod).check_digit
  end
end

#+(digit) ⇒ Object



19
20
21
# File 'lib/brid/algorithms/luhn.rb', line 19

def + digit
  self.class.new(@number += digit.to_s.scan(/\d/).join, :base => @base, :mode => @mod)
end

#==(other) ⇒ Object



27
28
29
# File 'lib/brid/algorithms/luhn.rb', line 27

def == other
  self.to_s == other.to_s
end

#check_digitObject



9
10
11
# File 'lib/brid/algorithms/luhn.rb', line 9

def check_digit
  sum / @mod
end

#to_sObject



23
24
25
# File 'lib/brid/algorithms/luhn.rb', line 23

def to_s
  @number.to_s
end