Class: TableTennis::Column
- Inherits:
-
Object
- Object
- TableTennis::Column
- 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
-
#c ⇒ Object
readonly
c is the column index.
-
#data ⇒ Object
readonly
c is the column index.
-
#header ⇒ Object
Returns the value of attribute header.
-
#name ⇒ Object
readonly
c is the column index.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #alignment ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(data, name, c) ⇒ Column
constructor
A new instance of Column.
- #map!(&block) ⇒ Object
- #measure ⇒ Object
- #truncate(stop) ⇒ Object
-
#type ⇒ Object
what type is this column? Sample 100 rows and guess.
Methods included from Util::Inspectable
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
#c ⇒ Object (readonly)
c is the column index
11 12 13 |
# File 'lib/table_tennis/column.rb', line 11 def c @c end |
#data ⇒ Object (readonly)
c is the column index
11 12 13 |
# File 'lib/table_tennis/column.rb', line 11 def data @data end |
#header ⇒ Object
Returns the value of attribute header.
12 13 14 |
# File 'lib/table_tennis/column.rb', line 12 def header @header end |
#name ⇒ Object (readonly)
c is the column index
11 12 13 |
# File 'lib/table_tennis/column.rb', line 11 def name @name end |
#width ⇒ Object
Returns the value of attribute width.
12 13 14 |
# File 'lib/table_tennis/column.rb', line 12 def width @width end |
Instance Method Details
#alignment ⇒ Object
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]) } |
#measure ⇒ Object
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 |
#type ⇒ Object
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 |