Class: Arrow::TableFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/arrow/table-formatter.rb

Overview

TODO: Almost codes should be implemented in Apache Arrow C++.

Direct Known Subclasses

TableListFormatter, TableTableFormatter

Defined Under Namespace

Classes: ColumnFormatter

Instance Method Summary collapse

Constructor Details

#initialize(table, options = {}) ⇒ TableFormatter

Returns a new instance of TableFormatter.



140
141
142
143
# File 'lib/arrow/table-formatter.rb', line 140

def initialize(table, options={})
  @table = table
  @options = options
end

Instance Method Details

#formatObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/arrow/table-formatter.rb', line 145

def format
  text = ""
  n_rows = @table.n_rows
  border = @options[:border] || 10

  head_limit = [border, n_rows].min

  tail_start = [border, n_rows - border].max
  tail_limit = n_rows - tail_start

  column_formatters = @table.columns.collect do |column|
    head_values = column.each.take(head_limit)
    if tail_limit > 0
      tail_values = column.reverse_each.take(tail_limit).reverse
    else
      tail_values = []
    end
    ColumnFormatter.new(column, head_values, tail_values)
  end

  format_header(text, column_formatters)
  return text if n_rows.zero?

  n_digits = (Math.log10(n_rows) + 1).truncate
  format_rows(text,
              column_formatters,
              column_formatters.collect(&:head_values).transpose,
              n_digits,
              0)
  return text if n_rows <= border


  if head_limit != tail_start
    format_ellipsis(text)
  end

  format_rows(text,
              column_formatters,
              column_formatters.collect(&:tail_values).transpose,
              n_digits,
              tail_start)

  text
end