Class: QRCode::Encoder::AlphanumericSegment

Inherits:
Segment
  • Object
show all
Defined in:
lib/qrcode/encoder/segment.rb

Overview

Alphanumeric segment - optimized for alphanumeric data

Constant Summary collapse

ALPHANUMERIC =
[
	"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
	"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", " ", "$",
	"%", "*", "+", "-", ".", "/", ":"
].freeze

Instance Attribute Summary

Attributes inherited from Segment

#data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Segment

#bit_size, build, #header_size

Constructor Details

#initialize(data) ⇒ AlphanumericSegment

Returns a new instance of AlphanumericSegment.



153
154
155
156
157
158
159
# File 'lib/qrcode/encoder/segment.rb', line 153

def initialize(data)
	data_str = data.to_s
	unless self.class.valid_data?(data_str)
		raise ArgumentError, "Not an alphanumeric string `#{data_str}`"
	end
	super(data_str)
end

Class Method Details

.valid_data?(data) ⇒ Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/qrcode/encoder/segment.rb', line 161

def self.valid_data?(data)
	(data.to_s.chars - ALPHANUMERIC).empty?
end

Instance Method Details

#content_sizeObject



169
170
171
# File 'lib/qrcode/encoder/segment.rb', line 169

def content_size
	(@data.size / 2.0).ceil * 11
end

#modeObject



165
166
167
# File 'lib/qrcode/encoder/segment.rb', line 165

def mode
	:mode_alpha_numk
end

#write(buffer) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/qrcode/encoder/segment.rb', line 173

def write(buffer)
	buffer.alphanumeric_encoding_start(@data.size)
	
	@data.size.times do |i|
		if i % 2 == 0
			if i == (@data.size - 1)
				value = ALPHANUMERIC.index(@data[i])
				buffer.put(value, 6)
			else
				value = (ALPHANUMERIC.index(@data[i]) * 45) + ALPHANUMERIC.index(@data[i + 1])
				buffer.put(value, 11)
			end
		end
	end
end