Class: TablePal::Table
- Inherits:
-
Object
- Object
- TablePal::Table
- Defined in:
- lib/table.rb
Instance Attribute Summary collapse
-
#cells_by_column ⇒ Object
readonly
Returns the value of attribute cells_by_column.
-
#cells_by_row_and_column ⇒ Object
readonly
Returns the value of attribute cells_by_row_and_column.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
-
#validations ⇒ Object
readonly
Returns the value of attribute validations.
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(options) ⇒ Object
-
#initialize(validations: false) ⇒ Table
constructor
A new instance of Table.
- #key(options) ⇒ Object
- #select_cells(column:) ⇒ Object
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_column ⇒ Object (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_column ⇒ Object (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 |
#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 |
#validations ⇒ Object (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( = {}) Validate.new(__method__, ) if validations row = [:row] column = [:column] ensure_cell_does_not_exist(row: row, column: column) Cell.new().tap do |cell| cells_by_column[column] << cell cells_by_row_and_column[key()] = cell end end |
#create_column(options = {}) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/table.rb', line 26 def create_column( = {}) Validate.new(__method__, ) if validations Column.new(.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( = {}) Validate.new(__method__, ) Row.new(.merge(table: self)).tap do |row| @rows << row end end |
#create_underline ⇒ Object
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
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() cells_by_row_and_column[key()] end |
#key(options) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/table.rb', line 53 def key() { row: [:row], column: [:column] } end |
#select_cells(column:) ⇒ Object
60 61 62 |
# File 'lib/table.rb', line 60 def select_cells(column:) cells_by_column[column] end |