Sha256: e2df376fc81b8b798b6a3d02a021638bf91eb2891ea675c85a2c1d72e7de8240

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module GS1
  module Barcode
    # Barcode for boxes in healthcare business.
    #
    class Healthcare < Base
      define_records GTIN, ExpirationDate, Batch, SerialNumber

      def to_s(level: AIDCMarketingLevels::ENHANCED)
        return unless valid?(level: level)

        [gtin.to_ai,
         expiration_date&.to_ai,
         batch&.to_ai,
         serial_number&.to_ai].compact.join
      end

      def valid?(level: AIDCMarketingLevels::ENHANCED)
        return false unless AIDCMarketingLevels::ALL.include?(level)

        validate(level)

        errors.empty?
      end

      private

      def validate(level)
        errors.clear

        validate_minimum
        return if level == AIDCMarketingLevels::MINIMUM

        validate_enhanced
        return if level == AIDCMarketingLevels::ENHANCED

        validate_highest
      end

      def validate_minimum
        errors << 'Invalid gtin' unless gtin.valid?
      end

      def validate_enhanced
        errors << 'Invalid batch' unless batch&.valid?
        errors << 'Invalid expiration date' unless expiration_date&.valid?
      end

      def validate_highest
        errors << 'Invalid serial number' unless serial_number&.valid?
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gs1-0.1.0 lib/gs1/barcode/healthcare.rb