Class: Barby::DataMatrix

Inherits:
Barcode2D show all
Defined in:
lib/barby/barcode/data_matrix.rb

Overview

Uses the dmtx library (gem install dmtx) to encode DataMatrix barcodes

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Barcode

#method_missing, #outputter_class_for, #outputter_for, outputters, register_outputter, #two_dimensional?, #valid?

Constructor Details

#initialize(data) ⇒ DataMatrix

Returns a new instance of DataMatrix.



13
14
15
# File 'lib/barby/barcode/data_matrix.rb', line 13

def initialize(data)
  self.data = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Barby::Barcode

Instance Attribute Details

#dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/barby/barcode/data_matrix.rb', line 10

def data
  @data
end

Instance Method Details

#bit?(x, y) ⇒ Boolean

NOTE: this method is not exposed via the gem so using send ahead of opening a PR to hopefully support:

github.com/mtgrosser/dmtx/blob/master/lib/dmtx/data_matrix.rb#L133-L135

Parameters:

  • x (Integer)

    x-coordinate

  • y (Integer)

    y-coordinate

Returns:

  • (Boolean)


66
67
68
# File 'lib/barby/barcode/data_matrix.rb', line 66

def bit?(x, y)
  encoder.send(:bit?, x, y)
end

#encoderObject



23
24
25
# File 'lib/barby/barcode/data_matrix.rb', line 23

def encoder
  @encoder ||= ::Dmtx::DataMatrix.new(data)
end

#encodingString

Converts the barcode to an array of lines where 0 is white and 1 is black.

Examples:

code = Barby::DataMatrix.new('humbaba')
code.encoding
# => [
# "10101010101010",
# "10111010000001",
# "11100101101100",
# "11101001110001",
# "11010101111110",
# "11100101100001",
# "11011001011110",
# "10011011010011",
# "11011010000100",
# "10101100101001",
# "11011100001100",
# "10101110110111",
# "11000001010100",
# "11111111111111",
# ]

Returns:

  • (String)


51
52
53
54
55
56
# File 'lib/barby/barcode/data_matrix.rb', line 51

def encoding
  width = encoder.width
  height = encoder.height

  height.times.map { |y| width.times.map { |x| bit?(x, y) ? '1' : '0' }.join }
end

#to_sString

The data being encoded.

Returns:

  • (String)


74
75
76
# File 'lib/barby/barcode/data_matrix.rb', line 74

def to_s
  data
end