class Gtin::GtinFormat

Enumeration of the different GTIN formats.

Constants

GTIN_12

GTIN-12, UPC-A, UPC-12. Standard version of the UPC code.

GTIN_13

GTIN-13, EAN-13. Primarily used in supermarkets to identify products at the point of sales.

GTIN_14

GTIN-14, EAN-14. Commonly used for traded goods.

GTIN_8

GTIN-8, EAN-8. The short version of EAN-13 for extremely small products.

Attributes

length[R]

@return the length of this GTIN format.

name[R]

@return the name of the constant, e.g. GTIN_8.

Public Class Methods

for_length(gtin_length) click to toggle source

Gets the GTIN format for the specified length.

@param gtin_length the length of the GTIN. @return the GTINFormat for the given length. @throws RangeError if the length does not match the length of any of the known formats.

# File lib/gtin/gtin_format.rb, line 68
def self.for_length(gtin_length)
  GtinFormat.values.each do |gtin_format|
    return gtin_format if gtin_length == gtin_format.length
  end
  fail(RangeError, "Length '#{gtin_length}' does not match the length of "\
    'any known GTIN formats')
end
new(name, length) click to toggle source

Prevent public instantiation

# File lib/gtin/gtin_format.rb, line 9
def initialize(name, length)
  @name = name
  @length = length
end
value_of(name) click to toggle source

Returns the enum constant of the specified enum type with the specified name.

@param the name of the constant to return. @return the enum constant of the specified enum type with the specified name. @throws RangeError if the name does not match the name of any of the known formats.

# File lib/gtin/gtin_format.rb, line 52
def self.value_of(name)
  GtinFormat.values.each do |gtin_format|
    return gtin_format if name == gtin_format.name
  end
  fail(RangeError, "Name '#{name}' does not match the name of any known "\
    'GTIN formats')
end
values() click to toggle source

@return an array of all of the GTINFormats.

# File lib/gtin/gtin_format.rb, line 38
def self.values
  [GTIN_8, GTIN_12, GTIN_13, GTIN_14]
end

Public Instance Methods

==(other) click to toggle source
# File lib/gtin/gtin_format.rb, line 93
def ==(other)
  self.equal?(other)
end
Also aliased as: eql?
eql?(other)
Alias for: ==
hash() click to toggle source
# File lib/gtin/gtin_format.rb, line 99
def hash
  13 + @length
end
to_s() click to toggle source

@return the name of the format, e.g. GTIN-12.

# File lib/gtin/gtin_format.rb, line 89
def to_s
  "GTIN-#{@length}"
end