Class: HighLine::List
Overview
List class with some convenience methods like #col_down.
Instance Attribute Summary collapse
-
#col_down_mode ⇒ Boolean
readonly
Content are distributed first by column in col down mode.
-
#cols ⇒ Integer
Number of columns for each list row.
-
#items ⇒ Array
readonly
Original given items argument.
-
#row_join_string ⇒ String
The String that will be used to join each cell of the list and stringfying it.
-
#transpose_mode ⇒ Boolean
readonly
Columns turn into rows in transpose mode.
Instance Method Summary collapse
-
#col_down ⇒ self
Slice the list by rows and transpose it.
-
#initialize(items, options = {}) ⇒ List
constructor
A new instance of List.
-
#list ⇒ Array
Returns an Array representation of the list in its current state.
-
#row_join_str_size ⇒ Integer
Returns the row join string size.
-
#slice_by_cols ⇒ self
Slice the list by cols based on the #cols param.
-
#slice_by_rows ⇒ self
Slice the list by rows.
-
#to_a ⇒ Array
Returns an Array representation of the list in its current state.
-
#to_s ⇒ String
Stringfies the list in its current state.
-
#transpose ⇒ self
Transpose the (already sliced by rows) list, turning its rows into columns.
Constructor Details
#initialize(items, options = {}) ⇒ List
Returns a new instance of List.
61 62 63 64 65 66 67 |
# File 'lib/highline/list.rb', line 61 def initialize(items, = {}) @items = items.to_a.dup.freeze @transpose_mode = .fetch(:transpose) { false } @col_down_mode = .fetch(:col_down) { false } @cols = .fetch(:cols) { 1 } build end |
Instance Attribute Details
#col_down_mode ⇒ Boolean (readonly)
Content are distributed first by column in col down mode.
53 54 55 |
# File 'lib/highline/list.rb', line 53 def col_down_mode @col_down_mode end |
#cols ⇒ Integer
Number of columns for each list row.
14 15 16 |
# File 'lib/highline/list.rb', line 14 def cols @cols end |
#items ⇒ Array (readonly)
Original given items argument. It’s frozen at initialization time and all later transformations will happen on #list.
10 11 12 |
# File 'lib/highline/list.rb', line 10 def items @items end |
#row_join_string ⇒ String
The String that will be used to join each cell of the list and stringfying it.
136 137 138 |
# File 'lib/highline/list.rb', line 136 def row_join_string @row_join_string ||= " " end |
#transpose_mode ⇒ Boolean (readonly)
Columns turn into rows in transpose mode.
32 33 34 |
# File 'lib/highline/list.rb', line 32 def transpose_mode @transpose_mode end |
Instance Method Details
#col_down ⇒ self
Slice the list by rows and transpose it.
81 82 83 84 85 |
# File 'lib/highline/list.rb', line 81 def col_down slice_by_rows transpose self end |
#list ⇒ Array
Returns an Array representation of the list in its current state.
112 113 114 |
# File 'lib/highline/list.rb', line 112 def list @list.dup end |
#row_join_str_size ⇒ Integer
Returns the row join string size. Useful for calculating the actual size of rendered list.
148 149 150 |
# File 'lib/highline/list.rb', line 148 def row_join_str_size row_join_string.size end |
#slice_by_cols ⇒ self
Slice the list by cols based on the #cols param.
97 98 99 100 |
# File 'lib/highline/list.rb', line 97 def slice_by_cols @list = items_sliced_by_cols self end |
#slice_by_rows ⇒ self
Slice the list by rows. The row count is calculated indirectly based on the #cols param and the items count.
90 91 92 93 |
# File 'lib/highline/list.rb', line 90 def slice_by_rows @list = items_sliced_by_rows self end |
#to_a ⇒ Array
Returns an Array representation of the list in its current state.
117 118 119 |
# File 'lib/highline/list.rb', line 117 def to_a list end |
#to_s ⇒ String
Stringfies the list in its current state. It joins each individual cell with the current #row_join_string between them. It joins each individual row with a newline character. So the returned String is suitable to be directly outputed to the screen, preserving row/columns divisions.
129 130 131 |
# File 'lib/highline/list.rb', line 129 def to_s list.map { |row| stringfy(row) }.join end |
#transpose ⇒ self
Transpose the (already sliced by rows) list,
turning its rows into columns.
72 73 74 75 76 77 |
# File 'lib/highline/list.rb', line 72 def transpose first_row = @list[0] other_rows = @list[1..-1] @list = first_row.zip(*other_rows) self end |