Class: Barby::Outputter

Inherits:
Object
  • Object
show all
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(barcode.encoding)
  end
end

Barcode#to_foo will now be available to all barcodes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(barcode)
  self.barcode = barcode
end

Instance Attribute Details

#barcodeObject

Returns the value of attribute barcode.



23
24
25
# File 'lib/barby/outputter.rb', line 23

def barcode
  @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