Sha256: db15347905913ff791d29cb20ca1e6cde09a52bfc3643ff3d5619e6499e33b92

Contents?: true

Size: 589 Bytes

Versions: 8

Compression:

Stored size: 589 Bytes

Contents

module GS1
  module Extensions
    # Extension for a GS1 GTIN. Ensures correct formating and validation.
    #
    module GTIN
      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

      # Default to GTIN-14 since it is the most common format.
      def to_s
        return unless data

        to_gtin_14
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gs1-1.1.0 lib/gs1/extensions/gtin.rb
gs1-1.0.0 lib/gs1/extensions/gtin.rb
gs1-0.1.8 lib/gs1/extensions/gtin.rb
gs1-0.1.7 lib/gs1/extensions/gtin.rb
gs1-0.1.6 lib/gs1/extensions/gtin.rb
gs1-0.1.5 lib/gs1/extensions/gtin.rb
gs1-0.1.3 lib/gs1/extensions/gtin.rb
gs1-0.1.2 lib/gs1/extensions/gtin.rb