Class: TablePal::Table

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validations: false) ⇒ Table

Returns a new instance of Table.



6
7
8
9
10
11
12
# File 'lib/table.rb', line 6

def initialize(validations: false)
  @validations             = validations
  @rows                    = []
  @columns                 = []
  @cells_by_column         = {}
  @cells_by_row_and_column = {}
end

Instance Attribute Details

#cells_by_columnObject (readonly)

Returns the value of attribute cells_by_column.



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

def cells_by_column
  @cells_by_column
end

#cells_by_row_and_columnObject (readonly)

Returns the value of attribute cells_by_row_and_column.



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

def cells_by_row_and_column
  @cells_by_row_and_column
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

#validationsObject (readonly)

Returns the value of attribute validations.



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

def validations
  @validations
end

Instance Method Details

#create_cell(options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/table.rb', line 35

def create_cell(options = {})
  Validate.new(__method__, options) if validations

  row    = options[:row]
  column = options[:column]

  ensure_cell_does_not_exist(row: row, column: column)

  Cell.new(options).tap do |cell|
    cells_by_column[column] << cell
    cells_by_row_and_column[key(options)] = cell
  end
end

#create_column(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/table.rb', line 26

def create_column(options = {})
  Validate.new(__method__, options) if validations

  Column.new(options.merge(table: self)).tap do |column|
    cells_by_column[column] = []
    @columns << column
  end
end

#create_row(options = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/table.rb', line 14

def create_row(options = {})
  Validate.new(__method__, options)

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

#create_underlineObject



22
23
24
# File 'lib/table.rb', line 22

def create_underline
  Row.new(table: self).cells_as_underline
end

#ensure_cell_does_not_exist(row:, column:) ⇒ Object

Raises:



64
65
66
67
68
69
70
71
72
# File 'lib/table.rb', line 64

def ensure_cell_does_not_exist(row:, column:)
  return unless validations

  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(options) ⇒ Object



49
50
51
# File 'lib/table.rb', line 49

def find_cell(options)
  cells_by_row_and_column[key(options)]
end

#key(options) ⇒ Object



53
54
55
56
57
58
# File 'lib/table.rb', line 53

def key(options)
  {
    row:    options[:row],
    column: options[:column]
  }
end

#select_cells(column:) ⇒ Object



60
61
62
# File 'lib/table.rb', line 60

def select_cells(column:)
  cells_by_column[column]
end