Sha256: a245f84a8a28546768c2f76c3cee05da8acfd9900d8b04c65f22bf9fb717949b

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 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

    # Create a service type from an integer

    def initialize(value)
      @value = value
    end

    # Validate the value.  Raises ArgumentError if out of range.
    # * +long_mailer_id+ - truthy if the mailer ID is long (9 digits).

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

    # Return true if +o+ is equal.  +o+ may be any object which ::coerce
    # can turn into a ServiceType.

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

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

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

    # Return the integer value of the service type

    def to_i
      @value
    end

    private

    NUM_DIGITS = 3 #:nodoc:

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
USPS-intelligent-barcode-0.2.2 lib/USPS-intelligent-barcode/ServiceType.rb
USPS-intelligent-barcode-0.2.1 lib/USPS-intelligent-barcode/ServiceType.rb