class Imb::MailerId

This class represents a mailer ID.

Constants

LONG_RANGE

The allowable range for a long (9-digit) mailer ID

RANGES

The list of all allowable ranges for a mailer ID

SHORT_RANGE

The allowable range for a short (6-digit) mailer ID

Public Class Methods

coerce(o) click to toggle source

Turn the argument into a MailerID if possible. Accepts:

  • {MailerId}

  • String

  • Integer

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

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

Construct an instance.

@param value [Integer]

# File lib/usps_intelligent_barcode/mailer_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/mailer_id.rb, line 44
def ==(o)
  MailerId.coerce(o).to_i == to_i
rescue ArgumentError
  false
end
long?() click to toggle source

Return true if this is a long (9 digit) mailer ID

# File lib/usps_intelligent_barcode/mailer_id.rb, line 58
def long?
  LONG_RANGE === @value
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/mailer_id.rb, line 80
def shift_and_add_to(target, long_mailer_id)
  target * 10 ** num_digits + to_i
end
to_i() click to toggle source

@return [Integer] The value of the mailer ID

# File lib/usps_intelligent_barcode/mailer_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/mailer_id.rb, line 67
def validate(long_mailer_id)
  unless in_range?
    raise ArgumentError, "Must be #{RANGES.join(' or ')}"
  end
end

Private Instance Methods

in_range?() click to toggle source

@!endgroup

# File lib/usps_intelligent_barcode/mailer_id.rb, line 88
def in_range?
  RANGES.any? do |range|
    range === @value
  end
end
num_digits() click to toggle source
# File lib/usps_intelligent_barcode/mailer_id.rb, line 94
def num_digits
  if long?
    9
  else
    6
  end
end