Class: Imb::BarcodeId
- Inherits:
-
Object
- Object
- Imb::BarcodeId
- Defined in:
- lib/USPS-intelligent-barcode/barcode_id.rb
Overview
This class represents a Barcode ID
Constant Summary collapse
- RANGE =
The allowable range of a barcode ID
0..94
- LSD_RANGE =
The allowable range of a barcode ID’s least significant digit
0..4
Internal collapse
-
#shift_and_add_to(target, long_mailer_id) ⇒ Integer
Add this object’s value to target, shifting it left as many digts as are needed to make room.
-
#validate(long_mailer_id) ⇒ Object
Validate the value.
Class Method Summary collapse
-
.coerce(o) ⇒ BarcodeId
Turn the argument into a BarcodeID if possible.
Instance Method Summary collapse
-
#==(o) ⇒ Object
Return true if this object is equal to o.
-
#initialize(value) ⇒ BarcodeId
constructor
Create a new BarcodeId.
-
#to_i ⇒ Integer
The integer value of the barcode ID.
Constructor Details
#initialize(value) ⇒ BarcodeId
Create a new BarcodeId
36 37 38 |
# File 'lib/USPS-intelligent-barcode/barcode_id.rb', line 36 def initialize(value) @value = value end |
Class Method Details
.coerce(o) ⇒ BarcodeId
Turn the argument into a BarcodeID if possible. Accepts any of:
-
String
-
Integer
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/USPS-intelligent-barcode/barcode_id.rb', line 20 def self.coerce(o) case o when BarcodeId o when String new(o.to_i) when Integer new(o) else raise ArgumentError, 'Cannot coerce to BarcodeId' end end |
Instance Method Details
#==(o) ⇒ Object
Return true if this object is equal to o
43 44 45 46 47 |
# File 'lib/USPS-intelligent-barcode/barcode_id.rb', line 43 def ==(o) BarcodeId.coerce(o).to_i == to_i rescue ArgumentError false end |
#shift_and_add_to(target, long_mailer_id) ⇒ Integer
Add this object’s value to target, shifting it left as many digts as are needed to make room.
76 77 78 79 80 81 82 |
# File 'lib/USPS-intelligent-barcode/barcode_id.rb', line 76 def shift_and_add_to(target, long_mailer_id) target *= 10 target += most_significant_digit target *= 5 target += least_significant_digit target end |
#to_i ⇒ Integer
Returns The integer value of the barcode ID.
51 52 53 |
# File 'lib/USPS-intelligent-barcode/barcode_id.rb', line 51 def to_i @value end |
#validate(long_mailer_id) ⇒ Object
Validate the value.
61 62 63 64 65 66 67 68 |
# File 'lib/USPS-intelligent-barcode/barcode_id.rb', line 61 def validate(long_mailer_id) unless RANGE === @value raise ArgumentError, "Must be #{RANGE}" end unless LSD_RANGE === least_significant_digit raise ArgumentError, "Least significant digit must be #{LSD_RANGE}" end end |