Class: Barcodes::Symbology::Base

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

Overview

Base class for all barcode symbologies.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Base

Creates a new barcode instance with given arguments. See class attributes for list of acceptable arguments.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/barcodes/symbology/base.rb', line 54

def initialize(args={})
  @data = '0123456789'
  @bar_width = 20
  @bar_height = 1000
  @alpha = 1.0
  @color = '000000'
  @caption_height = 180
  @caption_size = 167
  @captioned = true

  args.each do |k,v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end
end

Instance Attribute Details

#alphaObject

Alpha (transparency)



23
24
25
# File 'lib/barcodes/symbology/base.rb', line 23

def alpha
  @alpha
end

#bar_heightObject

Bar height in mils



20
21
22
# File 'lib/barcodes/symbology/base.rb', line 20

def bar_height
  @bar_height
end

#bar_widthObject

Bar width in mils



17
18
19
# File 'lib/barcodes/symbology/base.rb', line 17

def bar_width
  @bar_width
end

#caption_heightObject

Caption height in mils



29
30
31
# File 'lib/barcodes/symbology/base.rb', line 29

def caption_height
  @caption_height
end

#caption_sizeObject

Caption font size in mils



32
33
34
# File 'lib/barcodes/symbology/base.rb', line 32

def caption_size
  @caption_size
end

#captionedObject

Whether or not to print caption



35
36
37
# File 'lib/barcodes/symbology/base.rb', line 35

def captioned
  @captioned
end

#colorObject

Color in hex



26
27
28
# File 'lib/barcodes/symbology/base.rb', line 26

def color
  @color
end

#dataObject

Data to be encoded.



14
15
16
# File 'lib/barcodes/symbology/base.rb', line 14

def data
  @data
end

Class Method Details

.charsetObject

Returns the barcode symbologies character set as array of ASCII integer values. This method should be overridden by concrete subclass to provide character set for symbology.



40
41
42
43
# File 'lib/barcodes/symbology/base.rb', line 40

def self.charset
  # Should be overridden by subclass to provide charset
  [].collect {|c| c.bytes.to_a[0] }
end

.valuesetObject

Returns the values of the symbologies character set as array of encoded sets. This method should be overridden by concrete subclass to provide value set for symbology.



48
49
50
# File 'lib/barcodes/symbology/base.rb', line 48

def self.valueset
  []
end

Instance Method Details

#caption_dataObject

Returns the data to be printed in barcode caption. Could be overridden by concrete subclass to provide additional formatting.



72
73
74
# File 'lib/barcodes/symbology/base.rb', line 72

def caption_data
  self.data
end

#encoded_dataObject

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



84
85
86
87
88
89
90
91
92
# File 'lib/barcodes/symbology/base.rb', line 84

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

#formatted_dataObject

Returns the formatted barcode data to be encoded Could be overridden by concrete subclass to add additional formatting to data string.



79
80
81
# File 'lib/barcodes/symbology/base.rb', line 79

def formatted_data
  self.data
end

#heightObject

Returns the overall height of the barcode in mils.



111
112
113
# File 'lib/barcodes/symbology/base.rb', line 111

def height
  self.captioned ? self.caption_height + self.bar_height : self.bar_height
end

#quiet_zone_widthObject

Returns the symbologies quiet zone width in mils. Should be overridden by concrete subclass to provide quiet zone width if applicable.



97
98
99
# File 'lib/barcodes/symbology/base.rb', line 97

def quiet_zone_width
  0
end

#valid?Boolean

Determines whether or not the barcode data to be encoded is valid. Should be overridden in concrete subclass to provide validation.

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
# File 'lib/barcodes/symbology/base.rb', line 118

def valid?
  valid = self.data.length > 0 ? true : false

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

  return valid
end

#widthObject

Returns the overall width of the barcode in mils.



102
103
104
105
106
107
108
# File 'lib/barcodes/symbology/base.rb', line 102

def width
  if valid?
    (self.encoded_data.length * self.bar_width) + (self.quiet_zone_width * 2)
  else
    0
  end
end