Class: Mortadella::Horizontal

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

Overview

Horizontal makes it easy to build horizontal Cucumber-compatible tables.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers:, dry: []) ⇒ void

Parameters:

  • headers (Array<String>)

    The column headers for the table.

  • dry (Array<String>) (defaults to: [])

    Column names for which repeated values can be removed.



12
13
14
15
16
17
18
19
20
21
# File 'lib/mortadella/horizontal.rb', line 12

def initialize(headers:, dry: [])
  @headers = headers
  @dry = dry

  # The resulting Cucumber-compatible table structure.
  @table = [headers]

  # The previously added row
  @previous_row = nil
end

Instance Attribute Details

#tableArray<Array<String>> (readonly)

Returns The resulting Cucumber-compatible table structure.

Returns:

  • (Array<Array<String>>)

    The resulting Cucumber-compatible table structure.



7
8
9
# File 'lib/mortadella/horizontal.rb', line 7

def table
  @table
end

Instance Method Details

#<<(row) ⇒ void

This method returns an undefined value.

Adds the given row to the table.

Parameters:

  • row (Array<String>)

    The row data to add.



26
27
28
29
30
# File 'lib/mortadella/horizontal.rb', line 26

def <<(row)
  validate_row_length!(row)
  @table << dry_up(row)
  @previous_row = row
end

#empty?Boolean

Indicates whether the table contains no data rows (only a header row).

Returns:

  • (Boolean)


34
35
36
# File 'lib/mortadella/horizontal.rb', line 34

def empty?
  @table.size == 1
end

#keep_matching_columns(columns) ⇒ void

This method returns an undefined value.

Filters the table to keep only the specified columns.

Parameters:

  • columns (Array<String>)

    Names of the columns to keep.



41
42
43
44
45
46
47
# File 'lib/mortadella/horizontal.rb', line 41

def keep_matching_columns(columns)
  column_indices_to_drop(columns).sort.reverse_each do |column_index|
    @table.each do |row|
      row.delete_at column_index
    end
  end
end