class Imb::SerialNumber

This class represents the mail piece's serial number.

Public Class Methods

coerce(o) click to toggle source

Turn the argument into a SerialNumber if possible. Accepts:

  • {SerialNumber}

  • String

  • Integer

@return [SerialNumber]

# File lib/usps_intelligent_barcode/serial_number.rb, line 12
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
new(value) click to toggle source

Construct an instance.

@param [Integer] value

# File lib/usps_intelligent_barcode/serial_number.rb, line 28
def initialize(value)
  @value = value
end

Public Instance Methods

==(o) click to toggle source

Return true if this object is equal to o @param o [Object] Any object acceptable to {.coerce}

# File lib/usps_intelligent_barcode/serial_number.rb, line 34
def ==(o)
  SerialNumber.coerce(o).to_i == to_i
rescue ArgumentError
  false
end
shift_and_add_to(target, long_mailer_id) click to toggle source

Add this object's value to target, shifting it left as many digts as are needed to make room.

@param target [Integer] The target to be shifted and added to @param long_mailer_id [boolean] truthy if the mailer ID is long

(9 digits).

@return [Integer] The new value of the target

# 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() click to toggle source

@return [Integer] The value of the serial number

# File lib/usps_intelligent_barcode/serial_number.rb, line 41
def to_i
  @value
end
validate(long_mailer_id) click to toggle source

Validate the value.

@param long_mailer_id [boolean] truthy if the mailer ID is long

(9 digits).

@raise ArgumentError if invalid

# File lib/usps_intelligent_barcode/serial_number.rb, line 52
def validate(long_mailer_id)
  range = 0..max_value(long_mailer_id)
  unless range === @value
    raise ArgumentError, "Must be #{range}"
  end
end

Private Instance Methods

max_value(long_mailer_id) click to toggle source

@!endgroup

# File lib/usps_intelligent_barcode/serial_number.rb, line 74
def max_value(long_mailer_id)
  max_value = 10 ** num_digits(long_mailer_id) - 1
end
num_digits(long_mailer_id) click to toggle source
# File lib/usps_intelligent_barcode/serial_number.rb, line 78
def num_digits(long_mailer_id)
  if long_mailer_id
    6
  else
    9
  end
end