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.



162
163
164
165
# File 'lib/arrow/table-formatter.rb', line 162

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

Instance Method Details

#formatObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/arrow/table-formatter.rb', line 167

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(self, 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

#show_column_type?Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/arrow/table-formatter.rb', line 212

def show_column_type?
  @options.fetch(:show_column_type, true)
end