Sha256: 9647d18147633cb82a08ecf01d168b6bd5b1684b6319e7659f4c9a8949b0e8c5

Contents?: true

Size: 1.59 KB

Versions: 8

Compression:

Stored size: 1.59 KB

Contents

module Imb

  # This class represents a service type.

  class ServiceType

    # The valid range of a service type
    RANGE = 0..999

    # Turn the argument into a ServiceType if possible.  Accepts:
    # * {ServiceType}
    # * String
    # * Integer

    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

    # @param [Integer] value

    def initialize(value)
      @value = value
    end

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

    def ==(o)
      ServiceType.coerce(o).to_i == to_i
    rescue ArgumentError
      false
    end

    # @return [Integer] The value of the service type

    def to_i
      @value
    end

    # @!group Internal

    # Validate the value.
    # @param long_mailer_id truthy if the mailer ID is long (9 digits).
    # @raise ArgumentError if invalid

    def validate(long_mailer_id)
      unless (RANGE) === @value
        raise ArgumentError, "Must be #{RANGE}"
      end
    end

    # 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 truthy if the mailer ID is long (9 digits).
    # @return [Integer] The new value of the target

    def shift_and_add_to(target, long_mailer_id)
      target * 10 ** NUM_DIGITS + to_i
    end

    # @!endgroup

    private

    NUM_DIGITS = 3 #:nodoc:

  end

end

Version data entries

8 entries across 7 versions & 2 rubygems

Version Path
usps_intelligent_barcode-0.3.1 lib/usps_intelligent_barcode/service_type.rb
USPS-intelligent-barcode-0.2.7 lib/USPS-intelligent-barcode/service_type.rb
USPS-intelligent-barcode-0.2.6 lib/USPS-intelligent-barcode/service_type.rb
usps_intelligent_barcode-0.3.0 lib/USPS-intelligent-barcode/service_type.rb
usps_intelligent_barcode-0.3.0 lib/usps_intelligent_barcode/service_type.rb
USPS-intelligent-barcode-0.2.5 lib/USPS-intelligent-barcode/service_type.rb
USPS-intelligent-barcode-0.2.4 lib/USPS-intelligent-barcode/service_type.rb
USPS-intelligent-barcode-0.2.3 lib/USPS-intelligent-barcode/ServiceType.rb