Class Barby::Outputter
In: lib/barby/outputter.rb
Parent: Object
EAN13 Bookland EAN8 Barcode1D Code128 Code25 Code93 Code39 Barcode Barcode2D QrCode Pdf417 Code128A Code128B Code128C GS1128 Outputter CairoOutputter PngOutputter ASCIIOutputter RmagickOutputter PDFWriterOutputter SvgOutputter PrawnOutputter Code25IATA Code25Interleaved lib/barby/barcode/gs1_128.rb lib/barby/outputter/png_outputter.rb lib/barby/outputter/svg_outputter.rb lib/barby/outputter/rmagick_outputter.rb lib/barby/barcode.rb lib/barby/outputter/ascii_outputter.rb lib/barby/outputter.rb lib/barby/barcode/code_128.rb lib/barby/barcode/code_39.rb lib/barby/outputter/pdfwriter_outputter.rb lib/barby/barcode/code_93.rb lib/barby/outputter/prawn_outputter.rb lib/barby/barcode/ean_8.rb lib/barby/barcode/pdf_417.rb lib/barby/barcode/code_25_iata.rb lib/barby/outputter/cairo_outputter.rb lib/barby/barcode/ean_13.rb lib/barby/barcode/code_25.rb lib/barby/barcode/code_25_interleaved.rb lib/barby/barcode/qr_code.rb lib/barby/barcode/bookland.rb VERSION Barby dot/m_23_0.png

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

Methods

Attributes

barcode  [RW] 

Public Class methods

An outputter instance will have access to a Barcode

[Source]

    # File lib/barby/outputter.rb, line 27
27:     def initialize(barcode)
28:       self.barcode = barcode
29:     end

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

[Source]

    # 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

Private Instance methods

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]]

[Source]

     # 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

[Source]

    # 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

Returns the barcode‘s encoding. The encoding is cached and can be reloaded by passing true

[Source]

    # File lib/barby/outputter.rb, line 79
79:     def encoding(reload=false)#:doc:
80:       @encoding = barcode.encoding if reload
81:       @encoding ||= barcode.encoding
82:     end

[Source]

     # 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

[Validate]