class CukeModeler::Table

A class modeling a step’s table.

Attributes

rows[RW]

The row models that make up the table

Public Class Methods

new(source_text = nil) click to toggle source

Creates a new Table object and, if source_text is provided, populates the object.

@example

Table.new
Table.new("|value_1|value_2|\n|value_3|value_4|")

@param source_text [String] The Gherkin text that will be used to populate the model @raise [ArgumentError] If source_text is not a String @return [Table] A new Table instance

Calls superclass method CukeModeler::Model::new
# File lib/cuke_modeler/models/table.rb, line 25
def initialize(source_text = nil)
  @rows = []

  super
end

Public Instance Methods

children() click to toggle source

Returns the model objects that are children of this model. For a Table model, these would be any associated Row models.

@example

table.children

@return [Array<Row>] A collection of child models

# File lib/cuke_modeler/models/table.rb, line 38
def children
  rows
end
inspect(verbose: false) click to toggle source

See ‘Object#inspect`. Returns some basic information about the object, including its class, object ID, and its most meaningful attribute. For a Table model, this will be the rows of the table. If verbose is true, provides default Ruby inspection behavior instead.

@example

table.inspect
table.inspect(verbose: true)

@param verbose [Boolean] Whether or not to return the full details of

the object. Defaults to false.

@return [String] A string representation of this model

Calls superclass method CukeModeler::Model#inspect
# File lib/cuke_modeler/models/table.rb, line 66
def inspect(verbose: false)
  return super if verbose

  row_output = @rows&.collect { |row| row.cells.collect(&:value) }

  "#{super.chop} @rows: #{row_output.inspect}>"
end
to_s() click to toggle source

Returns a string representation of this model. For a Table model, this will be Gherkin text that is equivalent to the table being modeled.

@example

table.to_s

@return [String] A string representation of this model

# File lib/cuke_modeler/models/table.rb, line 49
def to_s
  rows.empty? ? '' : rows.collect { |row| row_output_string(row) }.join("\n")
end

Private Instance Methods

determine_buffer_size(index) click to toggle source
# File lib/cuke_modeler/models/table.rb, line 112
def determine_buffer_size(index)
  rows.collect { |row| row.cells[index].to_s.length }.max || 0
end
populate_model(parsed_table_data) click to toggle source
# File lib/cuke_modeler/models/table.rb, line 90
def populate_model(parsed_table_data)
  populate_row_models(parsed_table_data)
  populate_parsing_data(parsed_table_data)
  populate_source_location(parsed_table_data)
end
populate_row_models(parsed_table_data) click to toggle source
# File lib/cuke_modeler/models/table.rb, line 96
def populate_row_models(parsed_table_data)
  parsed_table_data['rows'].each do |row_data|
    @rows << build_child_model(Row, row_data)
  end
end
process_source(source_text) click to toggle source
# File lib/cuke_modeler/models/table.rb, line 78
def process_source(source_text)
  base_file_string = "# language: #{Parsing.dialect}
  #{dialect_feature_keyword}:
                        #{dialect_scenario_keyword}:
                          #{dialect_step_keyword} step\n"
  source_text = base_file_string + source_text

  parsed_file = Parsing.parse_text(source_text, 'cuke_modeler_stand_alone_table.feature')

  parsed_file['feature']['elements'].first['steps'].first['table']
end
row_output_string(row) click to toggle source
# File lib/cuke_modeler/models/table.rb, line 102
def row_output_string(row)
  row_text = '|'

  row.cells.count.times do |count|
    row_text << " #{row.cells[count].to_s.ljust(determine_buffer_size(count))} |"
  end

  row_text
end