Class: Barcodes::Symbology::Planet

Inherits:
Base
  • Object
show all
Defined in:
lib/barcodes/symbology/planet.rb

Overview

This class represents the PLANET symbology PLANET can encode only numbers 0-9

More info: en.wikipedia.org/wiki/Postal_Alpha_Numeric_Encoding_Technique

Instance Attribute Summary

Attributes inherited from Base

#alpha, #bar_height, #bar_width, #caption_height, #caption_size, #captioned, #color, #data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#caption_data, #quiet_zone_width

Constructor Details

#initialize(args = {}) ⇒ Planet

Creates a new Planet instance



33
34
35
36
37
38
39
# File 'lib/barcodes/symbology/planet.rb', line 33

def initialize(args={})
  unless args.has_key? :data
    args[:data] = '012345678999'
  end
  
  super(args)
end

Class Method Details

.charsetObject

PLANET character set



19
20
21
# File 'lib/barcodes/symbology/planet.rb', line 19

def self.charset
  ['0','1','2','3','4','5','6','7','8','9'].collect {|c| c.bytes.to_a[0] }
end

.valuesetObject

PLANET values set



24
25
26
27
28
29
30
# File 'lib/barcodes/symbology/planet.rb', line 24

def self.valueset
  [
    '00111','11100','11010','11001',
    '10110','10101','10011','01110',
    '01101','01011'
  ]
end

Instance Method Details

#checksumObject

Calculates the checksum using the provided data



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/barcodes/symbology/planet.rb', line 61

def checksum
  if self.valid?
    sum = 0
    @data.each_char do |char|
      sum += char.to_i
    end
    
    value = 10 - (sum % 10)
    if value == 10
      value = 0
    end

    if (0..9).include? value
      return value.to_s
    end
  end
end

#encoded_dataObject

Returns the barcode data encoded as 1’s and 0’s.



50
51
52
53
54
55
56
57
58
# File 'lib/barcodes/symbology/planet.rb', line 50

def encoded_data
  if self.valid?
    encoded_data = ''
    self.formatted_data.each_byte do |char|
      encoded_data += self._encode_character char
    end
    return '1' + encoded_data + '1'
  end
end

#formatted_dataObject

Returns data + checksum



42
43
44
45
46
47
# File 'lib/barcodes/symbology/planet.rb', line 42

def formatted_data
  checksum = self.checksum
  unless checksum.nil?
    @data + checksum
  end
end

#heightObject

Returns the overall height of the barcode in mils.



88
89
90
# File 'lib/barcodes/symbology/planet.rb', line 88

def height
  125
end

#valid?Boolean

Determines whether or not the barcode data to be encoded is valid. Data length must be 12 or 14 digits

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
103
# File 'lib/barcodes/symbology/planet.rb', line 95

def valid?
  @data.each_byte do |char|
    if self._encode_character(char).nil?
      return false
    end
  end

  return @data.length == 12 || @data.length == 14
end

#widthObject

Returns the overall width of the barcode in mils.



80
81
82
83
84
85
# File 'lib/barcodes/symbology/planet.rb', line 80

def width
  if self.valid?
    return (((self.encoded_data.length * 2) - 1) * 20)
  end
  return 0
end