Class: TablePal::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTable

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

#cellsObject (readonly)

Returns the value of attribute cells.



4
5
6
# File 'lib/table.rb', line 4

def cells
  @cells
end

#columnsObject (readonly)

Returns the value of attribute columns.



4
5
6
# File 'lib/table.rb', line 4

def columns
  @columns
end

#rowsObject (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(options = {})
  Validate.new(__method__, options)

  ensure_cell_does_not_exist(options.slice(:row, :column))

  Cell.new(options).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(options = {})
  Validate.new(__method__, options)

  Column.new(options.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(options = {})
  Validate.new(__method__, options)

  Row.new(options.merge(table: self)).tap do |row|
    @rows << row
  end
end

#create_underlineObject



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

Raises:



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