Class: EasyTable::TableBuilder

Inherits:
Object
  • Object
show all
Includes:
Components::Columns, Components::Spans
Defined in:
lib/easy_table/table_builder.rb

Instance Method Summary collapse

Methods included from Components::Spans

#span

Methods included from Components::Columns

#column

Constructor Details

#initialize(collection, template, options) ⇒ TableBuilder

Returns a new instance of TableBuilder.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/easy_table/table_builder.rb', line 9

def initialize(collection, template, options)
  @collection = collection
  @options = options
  tr_opts = @options.select { |k, _v| k =~ /^tr_.*/ }
  tr_opts.each { |k, _v| @options.delete(k) }
  @tr_opts = tr_opts.inject({}) do |h, e|
    k, v = *e
    h[k[3..-1]] = v
    h
  end

  @template = template
  @node = Tree::TreeNode.new('root')
end

Instance Method Details

#buildObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/easy_table/table_builder.rb', line 24

def build
  (:table, @options) do
    concat((:thead) do
      thead
    end)
    concat((:tbody) do
      @collection.each do |record|
        concat((:tr, tr_opts(record)) do
          node.each_leaf do |leaf|
            leaf.content.td(record)
          end
        end)
      end
    end)
  end
end