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(table_formatter, column, head_values, tail_values) ⇒ ColumnFormatter

Returns a new instance of ColumnFormatter.



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

def initialize(table_formatter, column, head_values, tail_values)
  @table_formatter = table_formatter
  @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_data_type_nameObject



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

def aligned_data_type_name
  @aligned_data_type_name ||=
    "%*s" % [aligned_name.size, formatted_data_type_name]
end

#aligned_nameObject



53
54
55
# File 'lib/arrow/table-formatter.rb', line 53

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

#data_typeObject



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

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

#format_value(value, width = 0) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/arrow/table-formatter.rb', line 60

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
    value = value.to_s
    if value.encoding == Encoding::ASCII_8BIT
      value = value.each_byte.collect {|byte| "%X" % byte}.join
    end
    "%-*s" % [width, value]
  end
end

#formatted_data_type_nameObject



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

def formatted_data_type_name
  @formatted_data_type_name ||= "(#{data_type.name})"
end

#nameObject



49
50
51
# File 'lib/arrow/table-formatter.rb', line 49

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