class Mortadella::Horizontal
Makes it easy to build DRY horizontal Cucumber-compatible tables
Attributes
table[R]
Public Class Methods
new(headers:, dry: [])
click to toggle source
# File lib/mortadella/horizontal.rb, line 11 def initialize headers:, dry: [] @headers = headers @dry = dry # The resulting Cucumber-compatible table structure @table = [headers] # The previously added row @previous_row = nil end
Public Instance Methods
<<(row)
click to toggle source
Adds the given row to the table
# File lib/mortadella/horizontal.rb, line 25 def << row @table << dry_up(row) @previous_row = row end
empty?()
click to toggle source
# File lib/mortadella/horizontal.rb, line 31 def empty? @table.size == 1 end
keep_matching_columns(columns)
click to toggle source
# File lib/mortadella/horizontal.rb, line 36 def keep_matching_columns columns columns_indeces_to_drop(columns).sort.reverse_each do |column_number| @table.each do |row| row.delete_at column_number end end end
Private Instance Methods
can_dry?(column_name)
click to toggle source
Returns whether the column with the given name can be dried up
# File lib/mortadella/horizontal.rb, line 50 def can_dry? column_name @dry.include? column_name end
columns_indeces_to_drop(columns)
click to toggle source
Returns the column indeces to drop to make this table have the given columns
# File lib/mortadella/horizontal.rb, line 56 def columns_indeces_to_drop columns result = [] headers = @table[0] headers.each_with_index do |header, i| result << i unless columns.include? header end result end
dry_up(row)
click to toggle source
Returns a dried up version of the given row based on the row that came before in the table
In a dried up row, any values that match the previous row are removed, stopping on the first difference
# File lib/mortadella/horizontal.rb, line 71 def dry_up row return row unless @previous_row row.clone.tap do |result| row.length.times do |i| if can_dry?(@headers[i]) && row[i] == @previous_row[i] result[i] = '' else break end end end end