class Imb::BarcodeId

This class represents a Barcode ID, one of the fields that is used to generate a barcode.

Constants

LSD_RANGE

The allowable range of a barcode ID's least significant digit

RANGE

The allowable range of a barcode ID

Public Class Methods

coerce(o) click to toggle source

Turn the argument into a BarcodeID if possible. Accepts any of:

  • {BarcodeId}

  • String

  • Integer

@return [BarcodeId] @raise [ArgumentError] If the argument cannot be coerced

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

Create a new BarcodeId

@param value [Integer] The barcode ID

# File lib/usps_intelligent_barcode/barcode_id.rb, line 37
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/barcode_id.rb, line 44
def ==(o)
  BarcodeId.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/barcode_id.rb, line 78
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() click to toggle source

@return [Integer] The integer value of the barcode ID

# File lib/usps_intelligent_barcode/barcode_id.rb, line 51
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/barcode_id.rb, line 62
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

Private Instance Methods

least_significant_digit() click to toggle source
# File lib/usps_intelligent_barcode/barcode_id.rb, line 94
def least_significant_digit
  @value % 10
end
most_significant_digit() click to toggle source

@!endgroup

# File lib/usps_intelligent_barcode/barcode_id.rb, line 90
def most_significant_digit
  @value / 10
end