Sha256: ea1cb9359761ff92b07c776bc383394ed0f77afeaa5367e7fe93e06a2121cb5c

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

module CukeModeler

  # A class modeling a step's table.

  class Table < Model

    include Parsing
    include Parsed
    include Sourceable


    # The row models that make up the table
    attr_accessor :rows


    # Creates a new Table object and, if *source_text* is provided, populates
    # the object.
    def initialize(source_text = nil)
      @rows = []

      super(source_text)

      if source_text
        parsed_table_data = parse_source(source_text)
        populate_table(self, parsed_table_data)
      end
    end

    # Returns the model objects that belong to this model.
    def children
      rows
    end

    # Returns a string representation of this model. For a table model,
    # this will be Gherkin text that is equivalent to the table being modeled.
    def to_s
      rows.empty? ? '' : rows.collect { |row| row_output_string(row) }.join("\n")
    end


    private


    def parse_source(source_text)
      base_file_string = "# language: #{Parsing.dialect}\n#{dialect_feature_keyword}:\n#{dialect_scenario_keyword}:\n#{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

    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

    def determine_buffer_size(index)
      rows.collect { |row| row.cells[index].to_s.length }.max || 0
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cuke_modeler-3.3.0 lib/cuke_modeler/models/table.rb
cuke_modeler-3.2.0 lib/cuke_modeler/models/table.rb
cuke_modeler-3.1.0 lib/cuke_modeler/models/table.rb
cuke_modeler-3.0.0 lib/cuke_modeler/models/table.rb