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.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/postrunner/FlexiTable.rb', line 132

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

  @current_section = :body
  @current_row = nil

  @frame = true

  @column_attributes = []

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#column_attributesObject (readonly)

Returns the value of attribute column_attributes.



130
131
132
# File 'lib/postrunner/FlexiTable.rb', line 130

def column_attributes
  @column_attributes
end

#frameObject (readonly)

Returns the value of attribute frame.



130
131
132
# File 'lib/postrunner/FlexiTable.rb', line 130

def frame
  @frame
end

Instance Method Details

#bodyObject



152
153
154
# File 'lib/postrunner/FlexiTable.rb', line 152

def body
  @current_section = :body
end

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



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/postrunner/FlexiTable.rb', line 164

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))
  end
  @current_row.cell(content, attributes)
end

#enable_frame(enabled) ⇒ Object



200
201
202
# File 'lib/postrunner/FlexiTable.rb', line 200

def enable_frame(enabled)
  @frame = enabled
end

#footObject



156
157
158
# File 'lib/postrunner/FlexiTable.rb', line 156

def foot
  @current_section = :foot
end

#headObject



148
149
150
# File 'lib/postrunner/FlexiTable.rb', line 148

def head
  @current_section = :head
end

#new_rowObject



160
161
162
# File 'lib/postrunner/FlexiTable.rb', line 160

def new_row
  @current_row = nil
end

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



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

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

#set_column_attributes(col_attributes) ⇒ Object



186
187
188
189
190
# File 'lib/postrunner/FlexiTable.rb', line 186

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

#set_row_attributes(row_attributes) ⇒ Object



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

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



219
220
221
222
223
224
225
# File 'lib/postrunner/FlexiTable.rb', line 219

def to_html(doc)
  doc.table {
    @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



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

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