Class: RQRCodeCore::QRNumeric

Inherits:
Object
  • Object
show all
Defined in:
lib/rqrcode_core/qrcode/qr_numeric.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ QRNumeric

Returns a new instance of QRNumeric.



9
10
11
12
13
14
15
# File 'lib/rqrcode_core/qrcode/qr_numeric.rb', line 9

def initialize( data )
  @mode = QRMODE[:mode_number]

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

  @data = data;
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



7
8
9
# File 'lib/rqrcode_core/qrcode/qr_numeric.rb', line 7

def mode
  @mode
end

Class Method Details

.valid_data?(data) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/rqrcode_core/qrcode/qr_numeric.rb', line 21

def self.valid_data? data
  data.each_char do |s|
    return false if NUMERIC.index(s).nil?
  end
  true
end

Instance Method Details

#get_lengthObject



17
18
19
# File 'lib/rqrcode_core/qrcode/qr_numeric.rb', line 17

def get_length
  @data.size
end

#write(buffer) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rqrcode_core/qrcode/qr_numeric.rb', line 28

def write( buffer)
  buffer.numeric_encoding_start(get_length)

  (@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