Class: Greentable::Table
- Inherits:
-
Object
- Object
- Greentable::Table
- Defined in:
- lib/greentable/greentable_table.rb
Instance Method Summary collapse
- #col(th = nil, opts = {}, &block) ⇒ Object
-
#initialize(parent, records, opts) ⇒ Table
constructor
A new instance of Table.
- #process(&block) ⇒ Object
- #subrow(opts = {}, &block) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(parent, records, opts) ⇒ Table
Returns a new instance of Table.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/greentable/greentable_table.rb', line 7 def initialize(parent, records, opts) @parent = parent @records = records defaults = Greentable.configuration.defaults.clone rescue {} @defaults_tr = (defaults.delete(:tr) || {}).deep_merge(opts.delete(:tr) || {}) @defaults_th = (defaults.delete(:th) || {}).deep_merge(opts.delete(:th) || {}) @defaults_td = (defaults.delete(:td) || {}).deep_merge(opts.delete(:td) || {}) @opts = defaults.deep_merge(opts) @tr_attributes = [] @th_attributes = [] @th_html = [] @td_attributes = [] @td_html = [] end |
Instance Method Details
#col(th = nil, opts = {}, &block) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/greentable/greentable_table.rb', line 30 def col(th = nil, opts = {}, &block) @th_html[@current_col] = th @th_attributes[@current_col] = opts.delete(:th) || {} @td_attributes[@current_col] = opts @td_html[@row_counter.i] ||= [] @td_html[@row_counter.i][@current_col] = @parent.capture(&block) @current_col += 1 return nil end |
#process(&block) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/greentable/greentable_table.rb', line 42 def process(&block) @row_counter = Counter.new(@records.size) @records.each do |record| @current_col = 0 block.call(self, record) @row_counter.inc end end |
#subrow(opts = {}, &block) ⇒ Object
24 25 26 27 28 |
# File 'lib/greentable/greentable_table.rb', line 24 def subrow(opts = {}, &block) return if capture_headers return if opts[:display_on] == :first && !@row_counter.first? return if opts[:display_on] == :last && !@row_counter.last? end |
#to_s ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/greentable/greentable_table.rb', line 52 def to_s ret = "" ret << "<table#{do_attributes(@opts)}>" unless @th_html.compact.empty? ret << "<thead>" ret << "<tr>" @th_html.each_with_index do |th,i| ret << "<th#{do_attributes(@th_attributes[i])}>#{th.is_a?(Proc) ? th.call.to_s : th}</th>" end ret << "</tr>" ret << "</thead>" end ret << "<tbody>" @row_counter.i.times do |i| ret << "<tr#{do_attributes(@defaults_tr.deep_merge(@tr_attributes[i]||{}))}>" @td_html[i].each do |td| ret << "<td#{do_attributes(@defaults_td.deep_merge(@td_attributes[i]||{}))}>#{td}</td>" end ret << "</tr>" end ret << "</tbody>" ret << "</table>" end |