Class: Imb::MailerId
- Inherits:
-
Object
- Object
- Imb::MailerId
- Defined in:
- lib/USPS-intelligent-barcode/mailer_id.rb
Overview
This class represents a mailer ID.
Constant Summary collapse
- SHORT_RANGE =
The allowable range for a short (6-digit) mailer ID
0..899_999
- LONG_RANGE =
The allowable range for a long (9-digit) mailer ID
900_000_000..999_999_999
- RANGES =
The list of all allowable ranges for a mailer ID
[SHORT_RANGE, LONG_RANGE]
Internal collapse
-
#long? ⇒ Boolean
Return true if this is a long (9 digit) mailer ID.
-
#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) ⇒ MailerId
Turn the argument into a MailerID if possible.
Instance Method Summary collapse
-
#==(o) ⇒ Object
Return true if this object is equal to o.
-
#initialize(value) ⇒ MailerId
constructor
A new instance of MailerId.
-
#to_i ⇒ Integer
The value of the mailer ID.
Constructor Details
#initialize(value) ⇒ MailerId
Returns a new instance of MailerId.
38 39 40 |
# File 'lib/USPS-intelligent-barcode/mailer_id.rb', line 38 def initialize(value) @value = value end |
Class Method Details
.coerce(o) ⇒ MailerId
Turn the argument into a MailerID if possible. Accepts:
-
String
-
Integer
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/USPS-intelligent-barcode/mailer_id.rb', line 23 def self.coerce(o) case o when MailerId o when String new(o.to_i) when Integer new(o) else raise ArgumentError, 'Cannot coerce to MailerId' end end |
Instance Method Details
#==(o) ⇒ Object
Return true if this object is equal to o
45 46 47 48 49 |
# File 'lib/USPS-intelligent-barcode/mailer_id.rb', line 45 def ==(o) MailerId.coerce(o).to_i == to_i rescue ArgumentError false end |
#long? ⇒ Boolean
Return true if this is a long (9 digit) mailer ID
61 62 63 |
# File 'lib/USPS-intelligent-barcode/mailer_id.rb', line 61 def long? LONG_RANGE === @value 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.
81 82 83 |
# File 'lib/USPS-intelligent-barcode/mailer_id.rb', line 81 def shift_and_add_to(target, long_mailer_id) target * 10 ** num_digits + to_i end |
#to_i ⇒ Integer
Returns The value of the mailer ID.
53 54 55 |
# File 'lib/USPS-intelligent-barcode/mailer_id.rb', line 53 def to_i @value end |
#validate(long_mailer_id) ⇒ Object
Validate the value.
69 70 71 72 73 |
# File 'lib/USPS-intelligent-barcode/mailer_id.rb', line 69 def validate(long_mailer_id) unless in_range? raise ArgumentError, "Must be #{RANGES.join(' or ')}" end end |