Sha256: 2338d4cec8ef3019273ae6ac0e9626204a97e874c383d3b8890b3eadbafaecfb

Contents?: true

Size: 992 Bytes

Versions: 3

Compression:

Stored size: 992 Bytes

Contents

module Imb

  class SerialNumber

    def self.coerce(o)
      case o
      when SerialNumber
        o
      when String
        new(o.to_i)
      when Integer
        new(o)
      else
        raise ArgumentError, 'Cannot coerce to SerialNumber'
      end
    end

    def initialize(value)
      @value = value
    end

    def validate(long_mailer_id)
      range = 0..max_value(long_mailer_id)
      unless range === @value
        raise ArgumentError, "Must be #{range}"
      end
    end

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

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

    protected

    def to_i
      @value
    end

    private

    def max_value(long_mailer_id)
      max_value = 10 ** num_digits(long_mailer_id) - 1
    end

    def num_digits(long_mailer_id)
      if long_mailer_id
        6
      else
        9
      end
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
USPS-intelligent-barcode-0.1.2 lib/USPS-intelligent-barcode/SerialNumber.rb
USPS-intelligent-barcode-0.1.1 lib/USPS-intelligent-barcode/SerialNumber.rb
USPS-intelligent-barcode-0.1.0 lib/USPS-intelligent-barcode/SerialNumber.rb