Class: TablePrint::Row

Inherits:
Object
  • Object
show all
Includes:
RowRecursion
Defined in:
lib/table_print/row_group.rb

Instance Attribute Summary collapse

Attributes included from RowRecursion

#children, #parent

Instance Method Summary collapse

Methods included from RowRecursion

#add_child, #add_children, #add_formatter, #child_count, #column_count, #column_for, #columns, #header, #horizontal_separator, #insert_children, #set_column, #width

Constructor Details

#initializeRow

Returns a new instance of Row.



136
137
138
139
# File 'lib/table_print/row_group.rb', line 136

def initialize
  super
  @cells = {}
end

Instance Attribute Details

#cellsObject (readonly)

Returns the value of attribute cells.



132
133
134
# File 'lib/table_print/row_group.rb', line 132

def cells
  @cells
end

Instance Method Details

#absorb_children(column_names, rollup) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/table_print/row_group.rb', line 186

def absorb_children(column_names, rollup)
  @children.each do |group|
    next unless can_absorb?(group)
    group.skip_first_row!

    column_names.collect do |name|
      next unless group.children and group.children.length > 0
      value = group.children.first.cells[name]
      rollup[name] = value if value
    end
  end
end

#apply_formatters(column_name, value) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/table_print/row_group.rb', line 211

def apply_formatters(column_name, value)
  column_name = column_name.to_s
  return value unless column_for(column_name)

  column = column_for(column_name)
  formatters = []
  formatters.concat(Array(column.formatters))

  formatters << TimeFormatter.new(column.time_format)
  formatters << NoNewlineFormatter.new
  formatters << FixedWidthFormatter.new(column_for(column_name).width)

  # successively apply the formatters for a column
  formatters.inject(value) do |inner_value, formatter|
    formatter.format(inner_value)
  end
end

#can_absorb?(group) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
232
233
234
# File 'lib/table_print/row_group.rb', line 229

def can_absorb?(group)
  return true if group.child_count == 1

  return false if @already_absorbed_a_multigroup
  @already_absorbed_a_multigroup = true # only call this method once
end

#collapse!Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/table_print/row_group.rb', line 147

def collapse!
  children.each(&:collapse!)  # depth-first. start collapsing from the bottom and work our way up.

  to_absorb = []
  children.each do |group|
    next unless can_absorb?(group)
    to_absorb << group
  end

  to_absorb.each do |absorbable_group|
    absorbable_row = absorbable_group.children.shift

    # missing associations create groups with no rows
    children.delete(absorbable_group) and next unless absorbable_row

    @cells.merge!(absorbable_row.cells)

    i = children.index(absorbable_group)
    children.delete(absorbable_group) if absorbable_group.children.empty?
    insert_children(i, absorbable_row.children) if absorbable_row.children.any?
  end
end

#formatObject



177
178
179
180
181
182
183
184
# File 'lib/table_print/row_group.rb', line 177

def format
  column_names = columns.collect(&:name)

  output = [column_names.collect { |name| apply_formatters(name, @cells[name]) }.join(" #{TablePrint::Config.separator} ")]
  output.concat @children.collect { |g| g.format }

  output.join("\n")
end

#raw_column_data(column_name) ⇒ Object



199
200
201
202
203
# File 'lib/table_print/row_group.rb', line 199

def raw_column_data(column_name)
  output = [@cells[column_name.to_s]]
  output << @children.collect { |g| g.raw_column_data(column_name) }
  output.flatten
end

#raw_column_namesObject



205
206
207
208
209
# File 'lib/table_print/row_group.rb', line 205

def raw_column_names
  output = [@cells.keys]
  output << @children.collect { |g| g.raw_column_names }
  output.flatten.uniq
end

#set_cell_values(values_hash) ⇒ Object



170
171
172
173
174
175
# File 'lib/table_print/row_group.rb', line 170

def set_cell_values(values_hash)
  values_hash.each do |k, v|
    @cells[k.to_s] = v
  end
  self
end

#vis(prefix = "") ⇒ Object

this is a development tool, to show the structure of the row/row_group tree



142
143
144
145
# File 'lib/table_print/row_group.rb', line 142

def vis(prefix="")
  puts "#{prefix}row #{cells.inspect.to_s}"
  children.each{|c| c.vis(prefix + "  ")}
end