class ColumnPack::ColumnPacker

Arranges HTML elements into a fixed number of columns.

Public Class Methods

new(total_columns, options = {}) click to toggle source

Uses a fixed number of columns (total_columns).

Options: :algorithm specify a different bin packing algorithm (default :best_fit_decreasing)

available algorithms are :best_fit_decreasing, :best_fit_increasing

:shuffle_in_col after packing columns, shuffle the elements in each column (defaults to true)

# File lib/column_pack/column_packer.rb, line 16
def initialize(total_columns, options = {})
  @bin_packer = BinPacker.new(total_columns, options)
end

Public Instance Methods

add(height, content) click to toggle source
# File lib/column_pack/column_packer.rb, line 20
def add(height, content)
  @bin_packer.add(height.to_i, content)
end
render() click to toggle source

Renders all elements into columns.

# File lib/column_pack/column_packer.rb, line 25
def render
  wrap(@bin_packer.bins).html_safe
end

Private Instance Methods

column(bin) click to toggle source
# File lib/column_pack/column_packer.rb, line 40
def column(bin)
  content_tag :div, :class => "column-pack-col" do
    capture do
      bin.each do |element|
        concat element(element)
      end
    end
  end
end
element(element) click to toggle source
# File lib/column_pack/column_packer.rb, line 50
def element(element)
  content_tag :div, element.html_safe, :class => "column-pack-element"
end
wrap(bins) click to toggle source
# File lib/column_pack/column_packer.rb, line 30
def wrap(bins)
  content_tag :div, :class => "column-pack-wrap" do
    capture do
      bins.each do |bin|
        concat column(bin)
      end
    end
  end
end