Class: Barby::Outputter
- Inherits:
-
Object
- Object
- Barby::Outputter
- Defined in:
- lib/barby/outputter.rb
Overview
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(.encoding)
end
end
Barcode#to_foo will now be available to all barcodes
Direct Known Subclasses
AsciiOutputter, CairoOutputter, HtmlOutputter, PDFWriterOutputter, PngOutputter, PrawnOutputter, RmagickOutputter, SvgOutputter
Instance Attribute Summary collapse
-
#barcode ⇒ Object
Returns the value of attribute barcode.
Class Method Summary collapse
-
.register(*method_names) ⇒ Object
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.
Instance Method Summary collapse
-
#boolean_groups(reload = false) ⇒ Object
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.
-
#booleans(reload = false) ⇒ Object
Converts the barcode’s encoding (a string containing 1s and 0s) to true and false values (1 == true == “black bar”).
-
#encoding(reload = false) ⇒ Object
Returns the barcode’s encoding.
-
#initialize(barcode) ⇒ Outputter
constructor
An outputter instance will have access to a Barcode.
- #two_dimensional? ⇒ Boolean
Constructor Details
#initialize(barcode) ⇒ Outputter
An outputter instance will have access to a Barcode
27 28 29 |
# File 'lib/barby/outputter.rb', line 27 def initialize() self. = end |
Instance Attribute Details
#barcode ⇒ Object
Returns the value of attribute barcode.
23 24 25 |
# File 'lib/barby/outputter.rb', line 23 def @barcode end |
Class Method Details
.register(*method_names) ⇒ Object
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
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/barby/outputter.rb', line 48 def self.register(*method_names) if method_names.first.is_a? Hash method_names.first.each do |name, method_name| Barcode.register_outputter(name, self, method_name) end else method_names.each do |name| Barcode.register_outputter(name, self, name) end end end |
Instance Method Details
#boolean_groups(reload = false) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/barby/outputter.rb', line 93 def boolean_groups(reload=false) if two_dimensional? encoding(reload).map do |line| line.scan(/1+|0+/).map do |group| [group[0,1] == '1', group.size] end end else encoding(reload).scan(/1+|0+/).map do |group| [group[0,1] == '1', group.size] end end end |
#booleans(reload = false) ⇒ Object
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
71 72 73 74 75 76 77 |
# File 'lib/barby/outputter.rb', line 71 def booleans(reload=false)#:doc: if two_dimensional? encoding(reload).map{|l| l.split(//).map{|c| c == '1' } } else encoding(reload).split(//).map{|c| c == '1' } end end |
#encoding(reload = false) ⇒ Object
Returns the barcode’s encoding. The encoding is cached and can be reloaded by passing true
82 83 84 85 |
# File 'lib/barby/outputter.rb', line 82 def encoding(reload=false)#:doc: @encoding = .encoding if reload @encoding ||= .encoding end |
#two_dimensional? ⇒ Boolean
61 62 63 |
# File 'lib/barby/outputter.rb', line 61 def two_dimensional? .respond_to?(:two_dimensional?) && .two_dimensional? end |