Class: Arrow::TableFormatter::ColumnFormatter

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

Constant Summary collapse

FLOAT_N_DIGITS =
10
FORMATTED_NULL =
"(null)"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column, head_values, tail_values) ⇒ ColumnFormatter

Returns a new instance of ColumnFormatter.



27
28
29
30
31
32
33
# File 'lib/arrow/table-formatter.rb', line 27

def initialize(column, head_values, tail_values)
  @column = column
  @head_values = head_values
  @tail_values = tail_values
  @sample_values = head_values + tail_values
  @field_value_widths = {}
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



23
24
25
# File 'lib/arrow/table-formatter.rb', line 23

def column
  @column
end

#head_valuesObject (readonly)

Returns the value of attribute head_values.



24
25
26
# File 'lib/arrow/table-formatter.rb', line 24

def head_values
  @head_values
end

#sample_valuesObject (readonly)

Returns the value of attribute sample_values.



26
27
28
# File 'lib/arrow/table-formatter.rb', line 26

def sample_values
  @sample_values
end

#tail_valuesObject (readonly)

Returns the value of attribute tail_values.



25
26
27
# File 'lib/arrow/table-formatter.rb', line 25

def tail_values
  @tail_values
end

Instance Method Details

#aligned_nameObject



43
44
45
# File 'lib/arrow/table-formatter.rb', line 43

def aligned_name
  @aligned_name ||= format_aligned_name(name, data_type, @sample_values)
end

#data_typeObject



35
36
37
# File 'lib/arrow/table-formatter.rb', line 35

def data_type
  @data_type ||= @column.data_type
end

#format_value(value, width = 0) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/arrow/table-formatter.rb', line 50

def format_value(value, width=0)
  case value
  when ::Time
    value.iso8601
  when Float
    "%*f" % [[width, FLOAT_N_DIGITS].max, value]
  when Integer
    "%*d" % [width, value]
  when Hash
    formatted_values = data_type.fields.collect do |field|
      field_name = field.name
      field_value_width = compute_field_value_width(field, @sample_values)
      formatted_name = format_value(field_name, 0)
      formatted_value = format_value(value[field_name], field_value_width)
      "#{formatted_name}: #{formatted_value}"
    end
    formatted = "{"
    formatted << formatted_values.join(", ")
    formatted << "}"
    "%-*s" % [width, formatted]
  when nil
    "%*s" % [width, FORMATTED_NULL]
  else
    "%-*s" % [width, value.to_s]
  end
end

#nameObject



39
40
41
# File 'lib/arrow/table-formatter.rb', line 39

def name
  @name ||= @column.name
end