Class: BankSlip::Barcode

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

Instance Method Summary collapse

Constructor Details

#initialize(value:, identification_code:, payment_date:, segment: 1, effective_reference: 6, free_digits: 0, product: 8) ⇒ Barcode

> options = 1, identification_code: 2, document_number: 10, payment_date: Date.new(2010, 3, 10) > barcode = BankSlip::Barcode.new(options)



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bank_slip/barcode.rb', line 26

def initialize(value:, identification_code:, payment_date:,
               segment: 1, effective_reference: 6,
               free_digits: 0, product: 8)
  @value               = value
  @segment             = segment
  @identification_code = identification_code
  @payment_date        = payment_date
  @product             = product
  @effective_reference = effective_reference
  @free_digits         = free_digits
  fail "identification_code #{identification_code} too long, #{identification_length} characters is the maximum allowed, for the segment #{segment}"  unless valid_identification_code?
end

Instance Method Details

#digitsObject

> barcode.digits

> “81640000000000100022010031000000000000000010”



41
42
43
# File 'lib/bank_slip/barcode.rb', line 41

def digits
  @digits ||= numbers.insert(3, digit(numbers).to_s)
end

#numerical_representationObject

> barcode.numerical_representation

> [“81640000000 5”, “00010002201 1”, “00310000000 3”, “00000000010 9”]



47
48
49
# File 'lib/bank_slip/barcode.rb', line 47

def numerical_representation
  @numerical_representation ||= digits.scan(/\d{11}/).collect {|p| "#{p} #{digit(p)}" }
end