Class: Gtin::GtinFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/gtin/gtin_format.rb

Overview

Enumeration of the different GTIN formats.

Constant Summary collapse

GTIN_8 =

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

new('GTIN_8', 8)
GTIN_12 =

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

new('GTIN_12', 12)
GTIN_13 =

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

new('GTIN_13', 13)
GTIN_14 =

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

new('GTIN_14', 14)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, length) ⇒ GtinFormat

Returns a new instance of GtinFormat.



9
10
11
12
# File 'lib/gtin/gtin_format.rb', line 9

def initialize(name, length)
  @name = name
  @length = length
end

Instance Attribute Details

#lengthObject (readonly)

Returns the length of this GTIN format.

Returns:

  • the length of this GTIN format.



84
85
86
# File 'lib/gtin/gtin_format.rb', line 84

def length
  @length
end

#nameObject (readonly)

Returns the name of the constant, e.g. GTIN_8.

Returns:

  • the name of the constant, e.g. GTIN_8.



79
80
81
# File 'lib/gtin/gtin_format.rb', line 79

def name
  @name
end

Class Method Details

.for_length(gtin_length) ⇒ Object

Gets the GTIN format for the specified length.

known formats.

Parameters:

  • gtin_length

    the length of the GTIN.

Returns:

  • the GTINFormat for the given length.



68
69
70
71
72
73
74
# 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

.value_of(name) ⇒ Object

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

name. known formats.

Parameters:

  • the

    name of the constant to return.

Returns:

  • the enum constant of the specified enum type with the specified



52
53
54
55
56
57
58
# 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

.valuesObject

Returns an array of all of the GTINFormats.

Returns:

  • an array of all of the GTINFormats.



38
39
40
# File 'lib/gtin/gtin_format.rb', line 38

def self.values
  [GTIN_8, GTIN_12, GTIN_13, GTIN_14]
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



93
94
95
# File 'lib/gtin/gtin_format.rb', line 93

def ==(other)
  self.equal?(other)
end

#hashObject



99
100
101
# File 'lib/gtin/gtin_format.rb', line 99

def hash
  13 + @length
end

#to_sObject

Returns the name of the format, e.g. GTIN-12.

Returns:

  • the name of the format, e.g. GTIN-12.



89
90
91
# File 'lib/gtin/gtin_format.rb', line 89

def to_s
  "GTIN-#{@length}"
end