| Class | Barby::Outputter |
| In: |
lib/barby/outputter.rb
|
| Parent: | Object |
An Outputter creates something from a barcode. That something can be anything, but is most likely a graphical representation of the barcode. Outputters can register methods on barcodes that will be associated with them.
The basic structure of an outputter class:
class FooOutputter < Barby::Outputter
register :to_foo
def to_too
do_something_with(barcode.encoding)
end
end
Barcode#to_foo will now be available to all barcodes
| barcode | [RW] |
Register one or more handler methods with this outputter Barcodes will then be able to use these methods to get the output from the outputter. For example, if you have an ImageOutputter, you could do:
register :to_png, :to_gif
You could then do aBarcode.to_png and get the result of that method. The class which registers the method will receive the barcode as the only argument, and the default implementation of initialize puts that into the barcode accessor.
You can also have different method names on the barcode and the outputter by providing a hash:
register :to_png => :create_png, :to_gif => :create_gif
# File lib/barby/outputter.rb, line 48
48: def self.register(*method_names)
49: if method_names.first.is_a? Hash
50: method_names.first.each do |name, method_name|
51: Barcode.register_outputter(name, self, method_name)
52: end
53: else
54: method_names.each do |name|
55: Barcode.register_outputter(name, self, name)
56: end
57: end
58: end
Collects continuous groups of bars and spaces (1 and 0) into arrays where the first item is true or false (1 or 0) and the second is the size of the group
For example, "1100111000" becomes [[true,2],[false,2],[true,3],[false,3]]
# File lib/barby/outputter.rb, line 90
90: def boolean_groups(reload=false)
91: if barcode.two_dimensional?
92: encoding(reload).map do |line|
93: line.scan(/1+|0+/).map do |group|
94: [group[0,1] == '1', group.size]
95: end
96: end
97: else
98: encoding(reload).scan(/1+|0+/).map do |group|
99: [group[0,1] == '1', group.size]
100: end
101: end
102: end
Converts the barcode‘s encoding (a string containing 1s and 0s) to true and false values (1 == true == "black bar")
If the barcode is 2D, each line will be converted to an array in the same way
# File lib/barby/outputter.rb, line 68
68: def booleans(reload=false)#:doc:
69: if barcode.two_dimensional?
70: encoding(reload).map{|l| l.split(//).map{|c| c == '1' } }
71: else
72: encoding(reload).split(//).map{|c| c == '1' }
73: end
74: end
# File lib/barby/outputter.rb, line 105
105: def with_options(options={})
106: original_options = options.inject({}) do |origs,pair|
107: if respond_to?(pair.first) && respond_to?("#{pair.first}=")
108: origs[pair.first] = send(pair.first)
109: send("#{pair.first}=", pair.last)
110: end
111: origs
112: end
113:
114: rv = yield
115:
116: original_options.each do |attribute,value|
117: send("#{attribute}=", value)
118: end
119:
120: rv
121: end