Sha256: 7c67ae890ce4ec21e1a0d3347fc618cab332c10ce5c144777898c0ff03a7aaf8

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

module TablePal
  class Table

    attr_reader :rows, :columns, :cells

    def initialize
      @rows    = []
      @columns = []
      @cells   = []
    end

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

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

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

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

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

    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

    def find_cell(row:, column:)
      cells.find { |cell| cell.row == row && cell.column == column }
    end

    def select_cells(column:)
      cells.select { |cell| cell.column == column }
    end

    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

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
table_pal-0.3.1 lib/table.rb
table_pal-0.3.0 lib/table.rb
table_pal-0.2.0 lib/table.rb