Class: TableHelper::TableBuilder

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper
Defined in:
lib/table_builder/table_builder.rb

Direct Known Subclasses

CalendarHelper::CalendarBuilder

Instance Method Summary collapse

Constructor Details

#initialize(objects, template, options) ⇒ TableBuilder

Returns a new instance of TableBuilder.

Raises:

  • (ArgumentError)


17
18
19
20
# File 'lib/table_builder/table_builder.rb', line 17

def initialize(objects, template, options)
  raise ArgumentError, "TableBuilder expects an Enumerable object but found #{objects.inspect}" unless objects.respond_to? :each
  @objects, @template, @options = objects, template, options
end

Instance Method Details

#body(*args) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
# File 'lib/table_builder/table_builder.rb', line 47

def body(*args)
  raise ArgumentError, "Missing block" unless block_given?
  options = options_from_hash(args)
  tbody do
    @objects.each { |c| yield(c) }
  end
end

#body_r(*args) ⇒ Object

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/table_builder/table_builder.rb', line 55

def body_r(*args)
  raise ArgumentError, "Missing block" unless block_given?
  options = options_from_hash(args)
  tbody do
    @objects.each { |c|
      concat(tag(:tr, options, true))
      yield(c)
      concat('</tr>'.html_safe)
    }
  end
end

#d(*args) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/table_builder/table_builder.rb', line 86

def d(*args)
  if block_given?
    concat(tag(:td, options_from_hash(args), true))
    yield
    concat('</td>')
  else
    content = args.shift
    (:td, content, options_from_hash(args))
  end
end

#h(*args) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/table_builder/table_builder.rb', line 75

def h(*args)
  if block_given?
    concat(tag(:th, options_from_hash(args), true))
    yield
    concat('</th>')
  else
    content = args.shift
    (:th, content, options_from_hash(args))
  end
end

#head(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/table_builder/table_builder.rb', line 22

def head(*args)
  if block_given?
    concat(tag(:thead, options_from_hash(args), true))
    yield
    concat('</thead>')
  else
    @num_of_columns = args.size
    (:thead,
      (:tr,
        args.collect { |c| (:th, c.html_safe)}.join('').html_safe
      )
    )
  end
end

#head_r(*args) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
# File 'lib/table_builder/table_builder.rb', line 37

def head_r(*args)
  raise ArgumentError, "Missing block" unless block_given?
  options = options_from_hash(args)
  head do
    concat(tag(:tr, options, true))
    yield
    concat('</tr>')
  end
end

#r(*args) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
# File 'lib/table_builder/table_builder.rb', line 67

def r(*args)
  raise ArgumentError, "Missing block" unless block_given?
  options = options_from_hash(args)
  tr(options) do
    yield
  end
end