Class: Mortadella::Vertical

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

Overview

Vertical makes it easy to build vertical Cucumber-compatible tables. Vertical tables display data as key-value pairs, with headers in the first column and corresponding values in the second column.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializevoid

Creates a new empty vertical table.



13
14
15
# File 'lib/mortadella/vertical.rb', line 13

def initialize
  @table = []
end

Instance Attribute Details

#tableArray<Array<String>> (readonly)

Returns The Cucumber-compatible table.

Returns:

  • (Array<Array<String>>)

    The Cucumber-compatible table.



9
10
11
# File 'lib/mortadella/vertical.rb', line 9

def table
  @table
end

Instance Method Details

#[]=(header, row) ⇒ void

This method returns an undefined value.

Adds a new row to the table with the given header and value.

Parameters:

  • header (String)

    The header (key) for the row.

  • row (String)

    The data (value) for the given header.

Raises:

  • (ArgumentError)

    If header or row is nil.



22
23
24
25
26
27
# File 'lib/mortadella/vertical.rb', line 22

def []=(header, row)
  raise ArgumentError, "Header cannot be nil" if header.nil?
  raise ArgumentError, "Row value cannot be nil" if row.nil?

  @table << [header, row]
end

#empty?Boolean

Indicates whether the table contains no rows.

Returns:

  • (Boolean)


31
32
33
# File 'lib/mortadella/vertical.rb', line 31

def empty?
  @table.empty?
end

#to_hHash<String, String>

Converts the table to a hash.

Returns:

  • (Hash<String, String>)

    Hash representation of the table.



37
38
39
# File 'lib/mortadella/vertical.rb', line 37

def to_h
  @table.to_h
end