Class: PostRunner::FlexiTable

Inherits:
Object
  • Object
show all
Defined in:
lib/postrunner/FlexiTable.rb

Defined Under Namespace

Classes: Attributes, Cell, Row

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ FlexiTable

Returns a new instance of FlexiTable.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/postrunner/FlexiTable.rb', line 160

def initialize(&block)
  @head_rows = []
  @body_rows = []
  @foot_rows = []
  @column_count = 0

  @current_section = :body
  @current_row = nil

  @frame = true
  @html_attrs = { :class => 'flexitable' }

  @column_attributes = []

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#column_attributesObject (readonly)

Returns the value of attribute column_attributes.



158
159
160
# File 'lib/postrunner/FlexiTable.rb', line 158

def column_attributes
  @column_attributes
end

#frameObject (readonly)

Returns the value of attribute frame.



158
159
160
# File 'lib/postrunner/FlexiTable.rb', line 158

def frame
  @frame
end

Instance Method Details

#bodyObject



185
186
187
# File 'lib/postrunner/FlexiTable.rb', line 185

def body
  @current_section = :body
end

#cell(content, attributes = {}) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/postrunner/FlexiTable.rb', line 202

def cell(content, attributes = {})
  if @current_row.nil?
    case @current_section
    when :head
      @head_rows
    when :body
      @body_rows
    when :foot
      @foot_rows
    else
      raise "Unknown section #{@current_section}"
    end << (@current_row = Row.new(self, @current_section))
  end
  @current_row.cell(content, attributes)
end

#enable_frame(enabled) ⇒ Object



238
239
240
# File 'lib/postrunner/FlexiTable.rb', line 238

def enable_frame(enabled)
  @frame = enabled
end

#footObject



189
190
191
# File 'lib/postrunner/FlexiTable.rb', line 189

def foot
  @current_section = :foot
end

#headObject



181
182
183
# File 'lib/postrunner/FlexiTable.rb', line 181

def head
  @current_section = :head
end

#new_rowObject



193
194
195
196
197
198
199
200
# File 'lib/postrunner/FlexiTable.rb', line 193

def new_row
  if @current_row && @head_rows[0] &&
     @current_row.length != @head_rows[0].length
    Log.fatal "Row has #{@current_row.length} cells instead of " +
              "#{@head_rows[0].length} cells in head row."
  end
  @current_row = nil
end

#row(cells, attributes = {}) ⇒ Object



218
219
220
221
222
# File 'lib/postrunner/FlexiTable.rb', line 218

def row(cells, attributes = {})
  cells.each { |c| cell(c) }
  set_row_attributes(attributes)
  new_row
end

#set_column_attributes(col_attributes) ⇒ Object



224
225
226
227
228
# File 'lib/postrunner/FlexiTable.rb', line 224

def set_column_attributes(col_attributes)
  col_attributes.each.with_index do |ca, idx|
    @column_attributes[idx] = Attributes.new(ca)
  end
end

#set_html_attrs(name, value) ⇒ Object



177
178
179
# File 'lib/postrunner/FlexiTable.rb', line 177

def set_html_attrs(name, value)
  @html_attrs[name] = value
end

#set_row_attributes(row_attributes) ⇒ Object



230
231
232
233
234
235
236
# File 'lib/postrunner/FlexiTable.rb', line 230

def set_row_attributes(row_attributes)
  unless @current_row
    raise "No current row. Use after first cell definition but before " +
          "new_row call."
  end
  @current_row.set_row_attributes(row_attributes)
end

#to_html(doc) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/postrunner/FlexiTable.rb', line 257

def to_html(doc)
  index_table

  doc.unique(:flexitable_style) {
    doc.head { doc.style(style) }
  }
  doc.table(@html_attrs) {
    @head_rows.each { |r| r.to_html(doc) }
    @body_rows.each { |r| r.to_html(doc) }
    @foot_rows.each { |r| r.to_html(doc) }
  }
end

#to_sObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/postrunner/FlexiTable.rb', line 242

def to_s
  index_table
  calc_terminal_columns

  s = frame_line_to_s
  s << rows_to_s(@head_rows)
  s << frame_line_to_s unless @head_rows.empty?
  s << rows_to_s(@body_rows)
  s << frame_line_to_s unless @body_rows.empty?
  s << rows_to_s(@foot_rows)
  s << frame_line_to_s unless @foot_rows.empty?

  s
end