module GS1::Extensions::GTIN

Extension for a GS1 GTIN. Ensures correct formating and validation.

Public Class Methods

included(base) click to toggle source
# File lib/gs1/extensions/gtin.rb, line 6
def self.included(base)
  base.define :check_digit
  base.define :length, allowed: [8, 12, 13, 14].freeze, barcode: 14

  base.allowed_lengths.each do |length|
    define_method "to_gtin_#{length}" do
      data.to_s.rjust(length, '0')
    end
  end
end

Public Instance Methods

to_s() click to toggle source

Default to GTIN-14 since it is the most common format.

# File lib/gs1/extensions/gtin.rb, line 18
def to_s
  return unless data

  to_gtin_14
end