Class: RussianPost::Barcode::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/russianpost/barcode/base.rb

Direct Known Subclasses

Domestic, International, Invalid

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(barcode) ⇒ Base

Returns a new instance of Base.



16
17
18
19
# File 'lib/russianpost/barcode/base.rb', line 16

def initialize(barcode)
  @barcode = barcode.strip.upcase
  @digits  = barcode.scan(/\d/).map { |d| d.to_i }
end

Instance Attribute Details

#barcodeObject (readonly)

Returns the value of attribute barcode.



10
11
12
# File 'lib/russianpost/barcode/base.rb', line 10

def barcode
  @barcode
end

#digitsObject (readonly)

Returns the value of attribute digits.



10
11
12
# File 'lib/russianpost/barcode/base.rb', line 10

def digits
  @digits
end

Class Method Details

.formatObject



12
13
14
# File 'lib/russianpost/barcode/base.rb', line 12

def self.format
  /.*/
end

Instance Method Details

#to_sObject



25
26
27
# File 'lib/russianpost/barcode/base.rb', line 25

def to_s
  barcode
end

#valid?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/russianpost/barcode/base.rb', line 21

def valid?
  (barcode =~ self.class.format || false) && digits.last == checkdigit
end