Class: Roebe::CustomTable

Inherits:
Object show all
Defined in:
lib/roebe/classes/custom_table/custom_table.rb

Constant Summary collapse

DEFAULT_LAYOUT =
#

DEFAULT_LAYOUT

#
'10x5'

Class Method Summary collapse

Instance Method Summary collapse

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

.[](i) ⇒ Object



189
190
191
192
# File 'lib/roebe/classes/custom_table/custom_table.rb', line 189

def self.[](i)
  _ = new(i, false)
  return _
end

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?

#

Returns:

  • (Boolean)


154
155
156
# File 'lib/roebe/classes/custom_table/custom_table.rb', line 154

def array?
  @array
end

#populate_tableObject

#

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

#resetObject

#

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

#runObject

#

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_tableObject 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