File: html.rb

package info (click to toggle)
ruby-rqrcode 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 420 kB
  • sloc: ruby: 753; makefile: 4
file content (51 lines) | stat: -rw-r--r-- 1,174 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true

module RQRCode
  module Export
    module HTML
      #
      # Use this module to HTML-ify the QR code if you just want the default HTML
      def as_html
        ["<table>", rows.as_html, "</table>"].join
      end

      private

      def rows
        Rows.new(@qrcode)
      end

      class Rows < Struct.new(:qr)
        def as_html
          rows.map(&:as_html).join
        end

        def rows
          qr.modules.each_with_index.map { |qr_module, row_index| Row.new(qr, qr_module, row_index) }
        end
      end

      class Row < Struct.new(:qr, :qr_module, :row_index)
        def as_html
          ["<tr>", cells.map(&:as_html).join, "</tr>"].join
        end

        def cells
          qr.modules.each_with_index.map { |qr_module, col_index| Cell.new(qr, col_index, row_index) }
        end
      end

      class Cell < Struct.new(:qr, :col_index, :row_index)
        def as_html
          "<td class=\"#{html_class}\"></td>"
        end

        def html_class
          qr.checked?(row_index, col_index) ? "black" : "white"
        end
      end
    end
  end
end

RQRCode::QRCode.send :include, RQRCode::Export::HTML