Class: Plushie::Widget::QrCode

Inherits:
Object
  • Object
show all
Defined in:
lib/plushie/widget/qr_code.rb

Overview

QR Code -- renders a QR code from a data string.

Props:

  • data (string) -- the data to encode.
  • cell_size (number) -- size of each QR module in pixels.
  • cell_color (string) -- color of dark modules.
  • background_color (string) -- color of light modules.
  • error_correction (symbol) -- :low, :medium, :quartile, :high.
  • alt (string) -- accessible label.
  • description (string) -- extended accessible description.
  • a11y (hash) -- accessibility overrides.

Examples:

qr = Plushie::Widget::QrCode.new("link", "https://example.com",
  cell_size: 6, error_correction: :high)
node = qr.build

Constant Summary collapse

PROPS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Supported property keys for this widget.

%i[data cell_size cell_color background_color error_correction
alt description a11y].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, data = nil, **opts) ⇒ QrCode

Returns a new instance of QrCode.

Parameters:

  • id (String)

    widget identifier

  • data (String) (defaults to: nil)

    data to encode

  • opts (Hash)

    optional properties



34
35
36
37
38
39
# File 'lib/plushie/widget/qr_code.rb', line 34

def initialize(id, data = nil, **opts)
  @id = id.to_s
  @data = data
  PROPS.each { |k| instance_variable_set(:"@#{k}", opts[k]) if opts.key?(k) }
  @data = opts[:data] if opts.key?(:data)
end

Instance Attribute Details

#a11yObject (readonly)

Returns the value of attribute a11y.



1
2
3
# File 'lib/plushie/widget/qr_code.rb', line 1

def a11y
  @a11y
end

#altObject (readonly)

Returns the value of attribute alt.



1
2
3
# File 'lib/plushie/widget/qr_code.rb', line 1

def alt
  @alt
end

#background_colorObject (readonly)

Returns the value of attribute background_color.



1
2
3
# File 'lib/plushie/widget/qr_code.rb', line 1

def background_color
  @background_color
end

#cell_colorObject (readonly)

Returns the value of attribute cell_color.



1
2
3
# File 'lib/plushie/widget/qr_code.rb', line 1

def cell_color
  @cell_color
end

#cell_sizeObject (readonly)

Returns the value of attribute cell_size.



1
2
3
# File 'lib/plushie/widget/qr_code.rb', line 1

def cell_size
  @cell_size
end

#dataObject (readonly)

Returns the value of attribute data.



1
2
3
# File 'lib/plushie/widget/qr_code.rb', line 1

def data
  @data
end

#descriptionObject (readonly)

Returns the value of attribute description.



1
2
3
# File 'lib/plushie/widget/qr_code.rb', line 1

def description
  @description
end

#error_correctionObject (readonly)

Returns the value of attribute error_correction.



1
2
3
# File 'lib/plushie/widget/qr_code.rb', line 1

def error_correction
  @error_correction
end

#idObject (readonly)

Returns the value of attribute id.



1
2
3
# File 'lib/plushie/widget/qr_code.rb', line 1

def id
  @id
end

Instance Method Details

#buildPlushie::Node

Returns:



48
49
50
51
52
53
54
55
# File 'lib/plushie/widget/qr_code.rb', line 48

def build
  props = {}
  PROPS.each do |key|
    val = instance_variable_get(:"@#{key}")
    Build.put_if(props, key, val)
  end
  Node.new(id: @id, type: "qr_code", props: props)
end