Class: Prawn::Qr::QRCode

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn-qr/qrcode.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, content) ⇒ QRCode

Returns a new instance of QRCode.



8
9
10
11
# File 'lib/prawn-qr/qrcode.rb', line 8

def initialize(document, content)
  @document = document
  @content = content
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



6
7
8
# File 'lib/prawn-qr/qrcode.rb', line 6

def content
  @content
end

#documentObject

Returns the value of attribute document.



6
7
8
# File 'lib/prawn-qr/qrcode.rb', line 6

def document
  @document
end

Instance Method Details

#drawObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/prawn-qr/qrcode.rb', line 26

def draw
  document.fill_color 'ffffff'
  document.rectangle(
    [horizontal_offset, vertical_offset + table_size],
    table_size, table_size
  )
  document.fill

  qrcode.reverse.each_with_index do |row, y|
    y += 1
    row.each_with_index do |dark, x|
      if dark
        document.fill_color = '000000'
        document.rectangle(
          [x * cell_size + horizontal_offset, y * cell_size + vertical_offset],
          cell_size, cell_size
        )
        document.fill
      end
    end
  end
end

#qrcodeObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/prawn-qr/qrcode.rb', line 13

def qrcode
  return @qrcode if @qrcode
  size = 1
  while(!@qrcode)
    begin
      @qrcode = RQRCode::QRCode.new(content, size: size)
    rescue RQRCode::QRCodeRunTimeError
      size += 1
    end
  end
  @qrcode = @qrcode.modules
end