Class: TablePal::Table
- Inherits:
-
Object
- Object
- TablePal::Table
- Defined in:
- lib/table.rb
Instance Attribute Summary collapse
-
#cells ⇒ Object
readonly
Returns the value of attribute cells.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #create_cell(options = {}) ⇒ Object
- #create_column(options = {}) ⇒ Object
- #create_row(options = {}) ⇒ Object
- #create_underline ⇒ Object
- #ensure_cell_does_not_exist(row:, column:) ⇒ Object
- #find_cell(row:, column:) ⇒ Object
-
#initialize ⇒ Table
constructor
A new instance of Table.
- #select_cells(column:) ⇒ Object
Constructor Details
#initialize ⇒ Table
Returns a new instance of Table.
6 7 8 9 10 |
# File 'lib/table.rb', line 6 def initialize @rows = [] @columns = [] @cells = [] end |
Instance Attribute Details
#cells ⇒ Object (readonly)
Returns the value of attribute cells.
4 5 6 |
# File 'lib/table.rb', line 4 def cells @cells end |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
4 5 6 |
# File 'lib/table.rb', line 4 def columns @columns end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
4 5 6 |
# File 'lib/table.rb', line 4 def rows @rows end |
Instance Method Details
#create_cell(options = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/table.rb', line 32 def create_cell( = {}) Validate.new(__method__, ) ensure_cell_does_not_exist(.slice(:row, :column)) Cell.new().tap do |cell| @cells << cell end end |
#create_column(options = {}) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/table.rb', line 24 def create_column( = {}) Validate.new(__method__, ) Column.new(.merge(table: self)).tap do |column| @columns << column end end |
#create_row(options = {}) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/table.rb', line 12 def create_row( = {}) Validate.new(__method__, ) Row.new(.merge(table: self)).tap do |row| @rows << row end end |
#create_underline ⇒ Object
20 21 22 |
# File 'lib/table.rb', line 20 def create_underline Row.new(table: self).cells_as_underline end |
#ensure_cell_does_not_exist(row:, column:) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/table.rb', line 50 def ensure_cell_does_not_exist(row:, column:) cell = find_cell(row: row, column: column) return unless cell raise TablePalError, "Cell with content: '#{cell.content}' already exists at this row and column." end |
#find_cell(row:, column:) ⇒ Object
42 43 44 |
# File 'lib/table.rb', line 42 def find_cell(row:, column:) cells.find { |cell| cell.row == row && cell.column == column } end |
#select_cells(column:) ⇒ Object
46 47 48 |
# File 'lib/table.rb', line 46 def select_cells(column:) cells.select { |cell| cell.column == column } end |