Class: Imb::ServiceType

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

Overview

This class represents a service type.

Constant Summary collapse

RANGE =

The valid range of a service type

0..999

Internal collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ ServiceType

Returns a new instance of ServiceType.

Parameters:

  • value (Integer)


30
31
32
# File 'lib/USPS-intelligent-barcode/service_type.rb', line 30

def initialize(value)
  @value = value
end

Class Method Details

.coerce(o) ⇒ Object

Turn the argument into a ServiceType if possible. Accepts:



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/USPS-intelligent-barcode/service_type.rb', line 15

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

Instance Method Details

#==(o) ⇒ Object

Return true if this object is equal to o

Parameters:

  • o (Object)

    Any object acceptable to coerce



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

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



67
68
69
# File 'lib/USPS-intelligent-barcode/service_type.rb', line 67

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

#to_iInteger

Returns The value of the service type.

Returns:

  • (Integer)

    The value of the service type



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

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



55
56
57
58
59
# File 'lib/USPS-intelligent-barcode/service_type.rb', line 55

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