Class: Imb::SerialNumber

Inherits:
Object
  • Object
show all
Defined in:
lib/USPS-intelligent-barcode/serial_number.rb

Overview

This class represents the mail piece’s serial number.

Internal collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ SerialNumber

Returns a new instance of SerialNumber.

Parameters:

  • value (Integer)


28
29
30
# File 'lib/USPS-intelligent-barcode/serial_number.rb', line 28

def initialize(value)
  @value = value
end

Class Method Details

.coerce(o) ⇒ SerialNumber

Turn the argument into a SerialNumber if possible. Accepts:

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/USPS-intelligent-barcode/serial_number.rb', line 13

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

Instance Method Details

#==(o) ⇒ Object

Return true if this object is equal to o

Parameters:

  • o (Object)

    Any object acceptable to coerce



35
36
37
38
39
# File 'lib/USPS-intelligent-barcode/serial_number.rb', line 35

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

#shift_and_add_to(target, long_mailer_id) ⇒ Integer

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

Parameters:

  • target (Integer)

    The target to be shifted and added to

  • long_mailer_id

    truthy if the mailer ID is long (9 digits).

Returns:

  • (Integer)

    The new value of the target



66
67
68
# File 'lib/USPS-intelligent-barcode/serial_number.rb', line 66

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

#to_iInteger

Returns The value of the serial number.

Returns:

  • (Integer)

    The value of the serial number



43
44
45
# File 'lib/USPS-intelligent-barcode/serial_number.rb', line 43

def to_i
  @value
end

#validate(long_mailer_id) ⇒ Object

Validate the value.

Parameters:

  • long_mailer_id

    truthy if the mailer ID is long (9 digits).

Raises:

  • ArgumentError if invalid



53
54
55
56
57
58
# File 'lib/USPS-intelligent-barcode/serial_number.rb', line 53

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