Sha256: f719cf7ab53b6ecea9cbe9b231ce10e7b35298f281a30771994768c356918fff

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

# EAN-8 Barcode 是8位条形码。
# https://en.wikipedia.org/wiki/EAN-8
class Unidom::ArticleNumber::Ean8Barcode < ActiveRecord::Base

  RESTRICTED_DISTRIBUTION_GS1_PREFIXES = (('020'..'029').to_a + ('040'..'049').to_a + ('200'..'299').to_a).freeze

  self.table_name = 'unidom_ean_8_barcodes'

  validates :code,           uniqueness: true,                  numericality: { only_integer: true }
  validates :gs1_prefix,     presence: true, length: { is: 3 }, numericality: { only_integer: true }
  validates :item_reference, presence: true, length: { is: 4 }, numericality: { only_integer: true }
  validates :check_digit,    presence: true, length: { is: 1 }, numericality: { only_integer: true }

  has_many :markings, class_name: 'Unidom::ArticleNumber::Marking'

  include Unidom::Common::Concerns::ModelExtension

  def code=(code)
    code = code.to_s
    write_attribute :code, code
    if code.present?
      write_attribute :gs1_prefix,     code[0..2]
      write_attribute :item_reference, code[3..6]
      write_attribute :check_digit,    code[7]
    end
  end

  def restricted_distribution?
    self.class::RESTRICTED_DISTRIBUTION_GS1_PREFIXES.include? gs1_prefix
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
unidom-article_number-1.0 app/models/unidom/article_number/ean8_barcode.rb
unidom-article_number-0.3 app/models/unidom/article_number/ean8_barcode.rb