Class: Ezframe::Ht::Table

Inherits:
Object show all
Defined in:
lib/ezframe/ht.rb

Overview

テーブルを生成するためのクラス

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matrix = nil) ⇒ Table

Returns a new instance of Table.



155
156
157
158
# File 'lib/ezframe/ht.rb', line 155

def initialize(matrix = nil)
  set(matrix) if matrix
  @matrix ||= []
end

Instance Attribute Details

#class_aObject

Returns the value of attribute class_a.



153
154
155
# File 'lib/ezframe/ht.rb', line 153

def class_a
  @class_a
end

#headerObject

Returns the value of attribute header.



153
154
155
# File 'lib/ezframe/ht.rb', line 153

def header
  @header
end

#matrixObject

Returns the value of attribute matrix.



153
154
155
# File 'lib/ezframe/ht.rb', line 153

def matrix
  @matrix
end

Instance Method Details

#add_row(row) ⇒ Object



164
165
166
# File 'lib/ezframe/ht.rb', line 164

def add_row(row)
  @matrix.push(row)
end

#set(matrix) ⇒ Object



160
161
162
# File 'lib/ezframe/ht.rb', line 160

def set(matrix)
  @matrix = matrix
end

#to_hObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/ezframe/ht.rb', line 168

def to_h
  table_class, tr_class, td_class = @class_a
  max_col = 0
  @matrix.each { |row| max_col = row.length if max_col < row.length }
  tr_a = @matrix.map do |row|
    add_attr = nil
    add_attr = { colspan: max_col - row.length + 1 } if row.length < max_col
    td_a = row.map do |v| 
      td = Ht.td(child: v) 
      td.add_class(td_class) if td_class
      td
    end
    td_a[0].update(add_attr) if add_attr
    tr = Ht.tr(class: tr_class, child: td_a)
    tr.add_class(tr_class) if tr_class
    tr
  end
  tr_a.unshift( Ht.thead(child: Ht.tr(child: @header.map {|v| Ht.th(child: v) }) )) if @header
  tb = Ht.table(child: tr_a)
  tb.add_class(table_class) if table_class
  tb
end