Class: QrCodeMaker::QRNumeric

Inherits:
Object
  • Object
show all
Defined in:
lib/qr_code_maker/encoder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ QRNumeric

Returns a new instance of QRNumeric.



520
521
522
523
# File 'lib/qr_code_maker/encoder.rb', line 520

def initialize(data)
  raise QRCodeArgumentError, "Not a numeric string `#{data}`" unless QRNumeric.valid_data?(data)
  @data = data
end

Class Method Details

.valid_data?(data) ⇒ Boolean

Returns:

  • (Boolean)


525
526
527
# File 'lib/qr_code_maker/encoder.rb', line 525

def self.valid_data?(data)
  (data.chars - NUMERIC).empty?
end

Instance Method Details

#write(buffer) ⇒ Object



529
530
531
532
533
534
535
536
537
538
# File 'lib/qr_code_maker/encoder.rb', line 529

def write(buffer)
  buffer.numeric_encoding_start(@data.size)
  @data.size.times do |i|
    if i % 3 == 0
      chars = @data[i, 3]
      bit_length = get_bit_length(chars.length)
      buffer.put(chars.to_i, bit_length)
    end
  end
end