class RQRCodeCore::QRNumeric

Constants

NUMBER_LENGTH

Public Class Methods

new(data) click to toggle source
# File lib/rqrcode_core/qrcode/qr_numeric.rb, line 7
def initialize(data)
  raise QRCodeArgumentError, "Not a numeric string `#{data}`" unless QRNumeric.valid_data?(data)

  @data = data
end
valid_data?(data) click to toggle source
# File lib/rqrcode_core/qrcode/qr_numeric.rb, line 13
def self.valid_data?(data)
  (data.chars - NUMERIC).empty?
end

Public Instance Methods

write(buffer) click to toggle source
# File lib/rqrcode_core/qrcode/qr_numeric.rb, line 17
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(get_code(chars), bit_length)
    end
  end
end

Private Instance Methods

get_bit_length(length) click to toggle source
# File lib/rqrcode_core/qrcode/qr_numeric.rb, line 37
def get_bit_length(length)
  NUMBER_LENGTH[length]
end
get_code(chars) click to toggle source
# File lib/rqrcode_core/qrcode/qr_numeric.rb, line 41
def get_code(chars)
  chars.to_i
end