Class: Barby::Barcode

Inherits:
Object
  • Object
show all
Defined in:
lib/barby/barcode.rb

Overview

The base class for all barcodes. It includes some method_missing magic that is used to find registered outputters.

The only interface requirement of a barcode class is that is has an encoding method that returns a string consisting of 1s and 0s representing the barcode’s “black” and “white” parts. One digit is the width of the “X dimension”; that is, “101100” represents a single-width bar followed by a single-width space, then a bar and a space twice that width.

Example implementation:

class StaticBarcode < Barby::Barcode1D

def encoding
 '101100111000111100001'
end

end

require ‘barby/outputter/ascii_outputter’ puts StaticBarcode.new.to_ascii(:height => 3)

# ## ### #### # # ## ### #### # # ## ### #### #

2D implementation:

class Static2DBarcode < Barby::Barcode2D

def encoding
  ['1010101', '010101110', '0001010100']
end

end

Direct Known Subclasses

Barcode1D, Barcode2D

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &b) ⇒ Object

:nodoc:



66
67
68
69
70
71
72
73
74
# File 'lib/barby/barcode.rb', line 66

def method_missing(name, *args, &b)#:nodoc:
  #See if an outputter has registered this method
  if self.class.outputters.include?(name)
    klass, method_name = self.class.outputters[name]
    klass.new(self).send(method_name, *args, &b)
  else
    super
  end
end

Class Method Details

.outputtersObject



91
92
93
# File 'lib/barby/barcode.rb', line 91

def outputters
  @@outputters ||= {}
end

.register_outputter(name, klass, method_name) ⇒ Object

Registers an outputter with name so that a call to name on a Barcode instance will be delegated to this outputter



97
98
99
# File 'lib/barby/barcode.rb', line 97

def register_outputter(name, klass, method_name)
  outputters[name] = [klass, method_name]
end

Instance Method Details

#encodingObject

Every barcode must have an encoding method. This method returns a string containing a series of 1 and 0, representing bars and spaces. One digit is the width of one “module” or X dimension.

If the barcode is 2D, it returns an array of strings representing each line in the barcode

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/barby/barcode.rb', line 44

def encoding
  raise NotImplementedError, 'Every barcode should implement this method'
end

#outputter_class_for(name) ⇒ Object

Get the outputter class object for name



84
85
86
# File 'lib/barby/barcode.rb', line 84

def outputter_class_for(name)
  self.class.outputters[name].first
end

#outputter_for(name, *a, &b) ⇒ Object

Returns an instantiated outputter for name if any outputter has registered that name



79
80
81
# File 'lib/barby/barcode.rb', line 79

def outputter_for(name, *a, &b)
  outputter_class_for(name).new(self, *a, &b)
end

#to_sObject



55
56
57
# File 'lib/barby/barcode.rb', line 55

def to_s
  self.class.name.split('::').last
end

#two_dimensional?Boolean

Is this barcode 2D?

Returns:

  • (Boolean)


61
62
63
# File 'lib/barby/barcode.rb', line 61

def two_dimensional?
  is_a? Barcode2D
end

#valid?Boolean

Is this barcode valid?

Returns:

  • (Boolean)


50
51
52
# File 'lib/barby/barcode.rb', line 50

def valid?
  false
end