-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
vertical.rb
38 lines (34 loc) · 963 Bytes
/
vertical.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true
module TTY
class Table
# A class representing table orientation
class Orientation
# A class responsible for vertical table transformation
class Vertical < Orientation
# Rotate table vertically
#
# @param [Table] table
#
# @return [nil]
#
# @api public
def transform(table)
table.rotate_vertical
end
# Slice horizontal table data into vertical
#
# @param [Table] table
#
# @api public
def slice(table)
header = table.header
rows_size = table.rows_size
head = header ? header : (0..rows_size).map { |n| (n + 1).to_s }
(0...rows_size).reduce([]) do |array, index|
array + head.zip(table.rows[index]).map { |row| table.to_row(row) }
end
end
end # Vertical
end # Orientation
end # Table
end # TTY