Class: TableTennis::Column

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, MemoWise, Util::Inspectable
Defined in:
lib/table_tennis/column.rb

Overview

A single column in a table. The data is actually stored in the rows, but it can be enumerated from here.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Inspectable

#inspect

Constructor Details

#initialize(data, name, c) ⇒ Column

Returns a new instance of Column.



15
16
17
18
19
20
21
# File 'lib/table_tennis/column.rb', line 15

def initialize(data, name, c)
  @name, @data, @c = name, data, c
  @header = config&.headers&.dig(name) || name.to_s
  if config&.titleize?
    @header = Util::Strings.titleize(@header)
  end
end

Instance Attribute Details

#cObject (readonly)

c is the column index



11
12
13
# File 'lib/table_tennis/column.rb', line 11

def c
  @c
end

#dataObject (readonly)

c is the column index



11
12
13
# File 'lib/table_tennis/column.rb', line 11

def data
  @data
end

#headerObject

Returns the value of attribute header.



12
13
14
# File 'lib/table_tennis/column.rb', line 12

def header
  @header
end

#nameObject (readonly)

c is the column index



11
12
13
# File 'lib/table_tennis/column.rb', line 11

def name
  @name
end

#widthObject

Returns the value of attribute width.



12
13
14
# File 'lib/table_tennis/column.rb', line 12

def width
  @width
end

Instance Method Details

#alignmentObject



38
39
40
41
42
43
# File 'lib/table_tennis/column.rb', line 38

def alignment
  case type
  when :float, :int then :right
  else :left
  end
end

#each(&block) ⇒ Object



23
24
25
26
27
# File 'lib/table_tennis/column.rb', line 23

def each(&block)
  return to_enum(__method__) unless block_given?
  rows.each { yield(_1[c]) }
  self
end

#map!(&block) ⇒ Object



29
# File 'lib/table_tennis/column.rb', line 29

def map!(&block) = rows.each { _1[c] = yield(_1[c]) }

#measureObject



50
51
52
# File 'lib/table_tennis/column.rb', line 50

def measure
  [2, max_by(&:length)&.length, header.length].max
end

#truncate(stop) ⇒ Object



45
46
47
48
# File 'lib/table_tennis/column.rb', line 45

def truncate(stop)
  @header = Util::Strings.truncate(header, stop)
  map! { Util::Strings.truncate(_1, stop) }
end

#typeObject

what type is this column? Sample 100 rows and guess. Will be nil if we aren’t sure.



32
33
34
35
# File 'lib/table_tennis/column.rb', line 32

def type
  samples = rows.sample(100).map { _1[c] }
  Util::Identify.identify_column(samples)
end