Class: Imb::BarcodeId

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

Overview

This class represents a Barcode ID

Constant Summary collapse

RANGE =

The allowable range of a barcode ID

0..94
LSD_RANGE =

The allowable range of a barcode ID’s least significant digit

0..4

Internal collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ BarcodeId

Create a new BarcodeId

Parameters:

  • value (Integer)

    The barcode ID



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

def initialize(value)
  @value = value
end

Class Method Details

.coerce(o) ⇒ BarcodeId

Turn the argument into a BarcodeID if possible. Accepts any of:

Returns:

Raises:

  • (ArgumentError)

    If the argument cannot be coerced



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/USPS-intelligent-barcode/barcode_id.rb', line 20

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

Instance Method Details

#==(o) ⇒ Object

Return true if this object is equal to o

Parameters:

  • o (Object)

    Any object acceptable to coerce



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

def ==(o)
  BarcodeId.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



76
77
78
79
80
81
82
# File 'lib/USPS-intelligent-barcode/barcode_id.rb', line 76

def shift_and_add_to(target, long_mailer_id)
  target *= 10
  target += most_significant_digit
  target *= 5
  target += least_significant_digit
  target
end

#to_iInteger

Returns The integer value of the barcode ID.

Returns:

  • (Integer)

    The integer value of the barcode ID



51
52
53
# File 'lib/USPS-intelligent-barcode/barcode_id.rb', line 51

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



61
62
63
64
65
66
67
68
# File 'lib/USPS-intelligent-barcode/barcode_id.rb', line 61

def validate(long_mailer_id)
  unless RANGE === @value
    raise ArgumentError, "Must be #{RANGE}"
  end
  unless LSD_RANGE === least_significant_digit
    raise ArgumentError, "Least significant digit must be #{LSD_RANGE}"
  end
end