Method: Barcode1DTools::EAN8#initialize

Defined in:
lib/barcode1dtools/ean8.rb

#initialize(value, options = {}) ⇒ EAN8

Create an EAN8 object with a given value. Options are :line_character, :space_character, and :checksum_included.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/barcode1dtools/ean8.rb', line 208

def initialize(value, options = {})

  @options = DEFAULT_OPTIONS.merge(options)

  # Can we encode this value?
  raise UnencodableCharactersError unless self.class.can_encode?(value, @options)

  if @options[:checksum_included]
    @encoded_string = sprintf('%08d', value.to_i)
    raise ChecksumError unless self.class.validate_check_digit_for(@encoded_string)
    md = @encoded_string.match(/^(\d+?)(\d)$/)
    @value, @check_digit = md[1], md[2].to_i
  else
    # need to add a checksum
    @value = sprintf('%07d', value.to_i)
    @check_digit = self.class.generate_check_digit_for(@value)
    @encoded_string = "#{@value}#{@check_digit}"
  end

  md = @value.match(/^(\d{3})(\d{4})/)
  @number_system, @product_code = md[1], md[2]
end