Class: Gtin::GtinFormat
- Inherits:
-
Object
- Object
- Gtin::GtinFormat
- 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
-
#length ⇒ Object
readonly
The length of this GTIN format.
-
#name ⇒ Object
readonly
The name of the constant, e.g.
Class Method Summary collapse
-
.for_length(gtin_length) ⇒ Object
Gets the GTIN format for the specified length.
-
.value_of(name) ⇒ Object
Returns the enum constant of the specified enum type with the specified name.
-
.values ⇒ Object
An array of all of the GTINFormats.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(name, length) ⇒ GtinFormat
constructor
A new instance of GtinFormat.
-
#to_s ⇒ Object
The name of the format, e.g.
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
#length ⇒ Object (readonly)
Returns the length of this GTIN format.
84 85 86 |
# File 'lib/gtin/gtin_format.rb', line 84 def length @length end |
#name ⇒ Object (readonly)
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.
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.
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 |
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 |
#hash ⇒ Object
99 100 101 |
# File 'lib/gtin/gtin_format.rb', line 99 def hash 13 + @length end |
#to_s ⇒ Object
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 |