Class: Roebe::CustomTable
- Defined in:
- lib/roebe/classes/custom_table/custom_table.rb
Constant Summary collapse
- DEFAULT_LAYOUT =
#
DEFAULT_LAYOUT
#
'10x5'
Class Method Summary collapse
-
.[](i) ⇒ Object
# === CustomTable ========================================================================= #.
Instance Method Summary collapse
-
#<<(i) ⇒ Object
# === << ========================================================================= #.
-
#array? ⇒ Boolean
(also: #array)
# === array? ========================================================================= #.
-
#initialize(i = DEFAULT_LAYOUT, run_already = true) ⇒ CustomTable
constructor
# === initialize ========================================================================= #.
-
#populate_table ⇒ Object
# === populate_table ========================================================================= #.
-
#remove_cells(i = '1: 3-6') ⇒ Object
# === remove_cells.
-
#reset ⇒ Object
# === reset (reset tag) ========================================================================= #.
-
#run ⇒ Object
# === run (run tag) ========================================================================= #.
-
#set_input(i = DEFAULT_LAYOUT) ⇒ Object
# === set_input ========================================================================= #.
-
#set_n_columns(i) ⇒ Object
# === set_n_columns ========================================================================= #.
-
#set_n_rows(i) ⇒ Object
# === set_n_rows ========================================================================= #.
-
#show_table ⇒ Object
(also: #show)
# === show_table ========================================================================= #.
Constructor Details
#initialize(i = DEFAULT_LAYOUT, run_already = true) ⇒ CustomTable
#
initialize
#
37 38 39 40 41 42 43 44 45 |
# File 'lib/roebe/classes/custom_table/custom_table.rb', line 37 def initialize( i = DEFAULT_LAYOUT, run_already = true ) reset set_input(i) populate_table run if run_already end |
Class Method Details
Instance Method Details
#<<(i) ⇒ Object
#
<<
#
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/roebe/classes/custom_table/custom_table.rb', line 161 def <<(i) i = i.to_s.dup @array.each_with_index {|row, outer_index| row.each_with_index {|column, inner_index| unless @added_element_already next if column.nil? if column.empty? @array[outer_index][inner_index] = i @added_element_already = true end end } } @added_element_already = false @n_entries += 1 end |
#array? ⇒ Boolean Also known as: array
#
array?
#
154 155 156 |
# File 'lib/roebe/classes/custom_table/custom_table.rb', line 154 def array? @array end |
#populate_table ⇒ Object
#
populate_table
#
99 100 101 102 103 104 105 106 107 |
# File 'lib/roebe/classes/custom_table/custom_table.rb', line 99 def populate_table @n_rows.times {|row| _ = [] @n_columns.times {|column| _ << '' } @array << _ } end |
#remove_cells(i = '1: 3-6') ⇒ Object
#
remove_cells
This method can remove cells from the main @array.
An input such as ‘1: 3-6’ means - find the first row, then delete the columns 3,4,5 and 6.
Example:
table.remove_cells('1: 3-6')
#
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/roebe/classes/custom_table/custom_table.rb', line 122 def remove_cells(i = '1: 3-6') if i.include? ':' splitted1 = i.split(':') entry = splitted1[0] entry = entry.to_i - 1 splitted2 = splitted1[1].split('-') start_position = splitted2[0].to_i - 1 end_position = splitted2[1].to_i - 1 @array[entry][ start_position .. end_position ] = Array.new((end_position - start_position) + 1) end end |
#reset ⇒ Object
#
reset (reset tag)
#
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/roebe/classes/custom_table/custom_table.rb', line 50 def reset # ======================================================================= # # === @array # ======================================================================= # @array = [] # ======================================================================= # # === @n_entries # ======================================================================= # @n_entries = 0 # ======================================================================= # # === @added_element_already # ======================================================================= # @added_element_already = false end |
#run ⇒ Object
#
run (run tag)
#
181 182 183 184 |
# File 'lib/roebe/classes/custom_table/custom_table.rb', line 181 def run # remove_cells # Testing purpose. show_table end |
#set_input(i = DEFAULT_LAYOUT) ⇒ Object
#
set_input
#
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/roebe/classes/custom_table/custom_table.rb', line 68 def set_input(i = DEFAULT_LAYOUT) i = i.first if i.is_a? Array i = DEFAULT_LAYOUT if i.nil? i = i.to_s.dup if i if i.include? 'x' splitted = i.split 'x' set_n_columns splitted[0] set_n_rows splitted[1] end @input = i end |
#set_n_columns(i) ⇒ Object
#
set_n_columns
#
83 84 85 86 |
# File 'lib/roebe/classes/custom_table/custom_table.rb', line 83 def set_n_columns(i) i = i.to_i @n_columns = i end |
#set_n_rows(i) ⇒ Object
#
set_n_rows
#
91 92 93 94 |
# File 'lib/roebe/classes/custom_table/custom_table.rb', line 91 def set_n_rows(i) i = i.to_i @n_rows = i end |
#show_table ⇒ Object Also known as: show
#
show_table
#
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/roebe/classes/custom_table/custom_table.rb', line 139 def show_table @array.each {|entry| entry.each {|inner_entry| if inner_entry print "#{inner_entry} |" else print '- |' # For now, a - denotes nil entries. end }; puts } end |