Class: Barby::HtmlOutputter

Inherits:
Outputter show all
Defined in:
lib/barby/outputter/html_outputter.rb

Overview

Outputs an HTML <table> containing cells for each module in the barcode.

This does NOT include any styling, you’re expected to add the relevant CSS yourself. The markup is simple: One <table> with class ‘barby-barcode’, one or more <tr class=“barby-row”> inside a <tbody> each containing <td class=“barby-cell”> for each module with the additional class “on” or “off”.

Example, let’s say the barcode.encoding == [‘101’, ‘010’] :

<table class="barby-barcode">
  <tbody>
     <tr class="barby-row">
       <td class="barby-cell on"></td>
       <td class="barby-cell off"></td>
       <td class="barby-cell on"></td>
     </tr>
     <tr class="barby-row">
       <td class="barby-cell off"></td>
       <td class="barby-cell on"></td>
       <td class="barby-cell off"></td>
     </tr>
  </tbody>
</table>

You could then style this with:

table.barby-barcode { border-spacing: 0; }
tr.barby-row {}
td.barby-cell { width: 3px; height: 3px; }
td.barby-cell.on { background: #000; }

Options:

:class_name - A class name that will be added to the <table> in addition to barby-barcode

Instance Attribute Summary collapse

Attributes inherited from Outputter

#barcode

Instance Method Summary collapse

Methods inherited from Outputter

#boolean_groups, #booleans, #encoding, #initialize, register, #two_dimensional?

Constructor Details

This class inherits a constructor from Barby::Outputter

Instance Attribute Details

#class_nameObject

Returns the value of attribute class_name.



43
44
45
# File 'lib/barby/outputter/html_outputter.rb', line 43

def class_name
  @class_name
end

Instance Method Details

#cells_for(booleans) ⇒ Object



66
67
68
# File 'lib/barby/outputter/html_outputter.rb', line 66

def cells_for(booleans)
  booleans.map{|b| b ? on_cell : off_cell }
end

#off_cellObject



78
79
80
# File 'lib/barby/outputter/html_outputter.rb', line 78

def off_cell
  '<td class="barby-cell off"></td>'
end

#on_cellObject



74
75
76
# File 'lib/barby/outputter/html_outputter.rb', line 74

def on_cell
  '<td class="barby-cell on"></td>'
end

#row_for(cells) ⇒ Object



70
71
72
# File 'lib/barby/outputter/html_outputter.rb', line 70

def row_for(cells)
  "<tr class=\"barby-row\">#{cells.join}</tr>"
end

#rowsObject



53
54
55
56
57
58
59
# File 'lib/barby/outputter/html_outputter.rb', line 53

def rows
  if barcode.two_dimensional?
    rows_for(booleans)
  else
    rows_for([booleans])
  end
end

#rows_for(boolean_groups) ⇒ Object



62
63
64
# File 'lib/barby/outputter/html_outputter.rb', line 62

def rows_for(boolean_groups)
  boolean_groups.map{|g| row_for(cells_for(g)) }
end

#startObject



82
83
84
# File 'lib/barby/outputter/html_outputter.rb', line 82

def start
  '<table class="barby-barcode'+(class_name ? " #{class_name}" : '')+'"><tbody>'
end

#stopObject



86
87
88
# File 'lib/barby/outputter/html_outputter.rb', line 86

def stop
  '</tbody></table>'
end

#to_html(options = {}) ⇒ Object



46
47
48
49
50
# File 'lib/barby/outputter/html_outputter.rb', line 46

def to_html(options = {})
  with_options options do
    start + rows.join + stop
  end
end