Sha256: 8ecb2ceaf774297a800ecbd0a54a7607e60a8206d3095513802ff1db4ede9360

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 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)

      return unless source_text

      parsed_table_data = parse_source(source_text)
      populate_table(self, parsed_table_data)
    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}
      #{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

    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

3 entries across 3 versions & 1 rubygems

Version Path
cuke_modeler-3.6.0 lib/cuke_modeler/models/table.rb
cuke_modeler-3.5.0 lib/cuke_modeler/models/table.rb
cuke_modeler-3.4.0 lib/cuke_modeler/models/table.rb