Class: Imb::SerialNumber
- Inherits:
-
Object
- Object
- Imb::SerialNumber
- Defined in:
- lib/USPS-intelligent-barcode/serial_number.rb
Overview
This class represents the mail piece’s serial number.
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) ⇒ SerialNumber
Turn the argument into a SerialNumber if possible.
Instance Method Summary collapse
-
#==(o) ⇒ Object
Return true if this object is equal to o.
-
#initialize(value) ⇒ SerialNumber
constructor
A new instance of SerialNumber.
-
#to_i ⇒ Integer
The value of the serial number.
Constructor Details
#initialize(value) ⇒ SerialNumber
Returns a new instance of SerialNumber.
28 29 30 |
# File 'lib/USPS-intelligent-barcode/serial_number.rb', line 28 def initialize(value) @value = value end |
Class Method Details
.coerce(o) ⇒ SerialNumber
Turn the argument into a SerialNumber if possible. Accepts:
-
String
-
Integer
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/USPS-intelligent-barcode/serial_number.rb', line 13 def self.coerce(o) case o when SerialNumber o when String new(o.to_i) when Integer new(o) else raise ArgumentError, 'Cannot coerce to SerialNumber' end end |
Instance Method Details
#==(o) ⇒ Object
Return true if this object is equal to o
35 36 37 38 39 |
# File 'lib/USPS-intelligent-barcode/serial_number.rb', line 35 def ==(o) SerialNumber.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.
66 67 68 |
# File 'lib/USPS-intelligent-barcode/serial_number.rb', line 66 def shift_and_add_to(target, long_mailer_id) target * 10 ** num_digits(long_mailer_id) + to_i end |
#to_i ⇒ Integer
Returns The value of the serial number.
43 44 45 |
# File 'lib/USPS-intelligent-barcode/serial_number.rb', line 43 def to_i @value end |
#validate(long_mailer_id) ⇒ Object
Validate the value.
53 54 55 56 57 58 |
# File 'lib/USPS-intelligent-barcode/serial_number.rb', line 53 def validate(long_mailer_id) range = 0..max_value(long_mailer_id) unless range === @value raise ArgumentError, "Must be #{range}" end end |