class Imb::ServiceType

This class represents a service type.

Constants

RANGE

The valid range of a service type

Public Class Methods

coerce(o) click to toggle source

Turn the argument into a ServiceType if possible. Accepts:

  • {ServiceType}

  • String

  • Integer

# File lib/usps_intelligent_barcode/service_type.rb, line 13
def self.coerce(o)
  case o
  when ServiceType
    o
  when String
    new(o.to_i)
  when Integer
    new(o)
  else
    raise ArgumentError, 'Cannot coerce to ServiceType'
  end
end
new(value) click to toggle source

@param value [Integer]

# File lib/usps_intelligent_barcode/service_type.rb, line 27
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/service_type.rb, line 33
def ==(o)
  ServiceType.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 [Integer] target 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/service_type.rb, line 64
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 service type

# File lib/usps_intelligent_barcode/service_type.rb, line 40
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/service_type.rb, line 51
def validate(long_mailer_id)
  unless (RANGE) === @value
    raise ArgumentError, "Must be #{RANGE}"
  end
end