Class: Imb::MailerId

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

Overview

This class represents a mailer ID.

Constant Summary collapse

SHORT_RANGE =

The allowable range for a short (6-digit) mailer ID

0..899_999
LONG_RANGE =

The allowable range for a long (9-digit) mailer ID

900_000_000..999_999_999
RANGES =

The list of all allowable ranges for a mailer ID

[SHORT_RANGE, LONG_RANGE]

Internal collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ MailerId

Returns a new instance of MailerId.

Parameters:

  • value (Integer)

38
39
40
# File 'lib/USPS-intelligent-barcode/mailer_id.rb', line 38

def initialize(value)
  @value = value
end

Class Method Details

.coerce(o) ⇒ MailerId

Turn the argument into a MailerID if possible. Accepts:

Returns:

Raises:

  • (ArgumentError)

    If the argument cannot be coerced


23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/USPS-intelligent-barcode/mailer_id.rb', line 23

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

Instance Method Details

#==(o) ⇒ Object

Return true if this object is equal to o

Parameters:

  • o (Object)

    Any object acceptable to coerce


45
46
47
48
49
# File 'lib/USPS-intelligent-barcode/mailer_id.rb', line 45

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

#long?Boolean

Return true if this is a long (9 digit) mailer ID

Returns:

  • (Boolean)

61
62
63
# File 'lib/USPS-intelligent-barcode/mailer_id.rb', line 61

def long?
  LONG_RANGE === @value
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


81
82
83
# File 'lib/USPS-intelligent-barcode/mailer_id.rb', line 81

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

#to_iInteger

Returns The value of the mailer ID.

Returns:

  • (Integer)

    The value of the mailer ID


53
54
55
# File 'lib/USPS-intelligent-barcode/mailer_id.rb', line 53

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


69
70
71
72
73
# File 'lib/USPS-intelligent-barcode/mailer_id.rb', line 69

def validate(long_mailer_id)
  unless in_range?
    raise ArgumentError, "Must be #{RANGES.join(' or ')}"
  end
end