Method: Barcode1DTools::Matrix2of5#initialize

Defined in:
lib/barcode1dtools/matrix2of5.rb

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

Create a new Matrix2of5 object with a given value. Options are :line_character, :space_character, :w_character, :n_character, :checksum_included, and :skip_checksum.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/barcode1dtools/matrix2of5.rb', line 177

def initialize(value, options = {})

  @options = DEFAULT_OPTIONS.merge(options)

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

  @value = value.to_s

  if @options[:skip_checksum]
    @encoded_string = value.to_s
    @value = value.to_s
    @check_digit = nil
  elsif @options[:checksum_included]
    @encoded_string = value.to_s
    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
    @value = value.to_s
    @check_digit = self.class.generate_check_digit_for(@value)
    @encoded_string = "#{@value}#{@check_digit}"
  end
end