class Cucumber::Core::Ast::DataTable

Step Definitions that match a plain text Step with a multiline argument table will receive it as an instance of DataTable. A DataTable object holds the data of a table parsed from a feature file and lets you access and manipulate the data in different ways.

For example:

Given I have:
  | a | b |
  | c | d |

And a matching StepDefinition:

Given /I have:/ do |table|
  data = table.raw
end

This will store [['a', 'b'], ['c', 'd']] in the data variable.

Attributes

raw[R]

Public Class Methods

new(rows, location) click to toggle source

Creates a new instance. raw should be an Array of Array of String or an Array of Hash You don't typically create your own DataTable objects - Cucumber will do it internally and pass them to your Step Definitions.

# File lib/cucumber/core/ast/data_table.rb, line 35
def initialize(rows, location)
  raw = ensure_array_of_array(rows)
  verify_rows_are_same_length(raw)
  @raw = raw.freeze
  @location = location
end

Public Instance Methods

==(other) click to toggle source
# File lib/cucumber/core/ast/data_table.rb, line 84
def ==(other)
  other.class == self.class && raw == other.raw
end
data_table?() click to toggle source
# File lib/cucumber/core/ast/data_table.rb, line 47
def data_table?
  true
end
doc_string?() click to toggle source
# File lib/cucumber/core/ast/data_table.rb, line 51
def doc_string?
  false
end
dup() click to toggle source

Creates a copy of this table

# File lib/cucumber/core/ast/data_table.rb, line 57
def dup
  self.class.new(raw.dup, location)
end
inspect() click to toggle source
# File lib/cucumber/core/ast/data_table.rb, line 88
def inspect
  %{#<#{self.class} #{raw.inspect} (#{location})>}
end
map(&block) click to toggle source
# File lib/cucumber/core/ast/data_table.rb, line 76
def map(&block)
  new_raw = raw.map do |row|
    row.map(&block)
  end

  self.class.new(new_raw, location)
end
to_step_definition_arg() click to toggle source
# File lib/cucumber/core/ast/data_table.rb, line 43
def to_step_definition_arg
  dup
end
transpose() click to toggle source

Returns a new, transposed table. Example:

| a | 7 | 4 |
| b | 9 | 2 |

Gets converted into the following:

| a | b |
| 7 | 9 |
| 4 | 2 |
# File lib/cucumber/core/ast/data_table.rb, line 72
def transpose
  self.class.new(raw.transpose, location)
end

Private Instance Methods

description_for_visitors() click to toggle source
# File lib/cucumber/core/ast/data_table.rb, line 111
def description_for_visitors
  :data_table
end
ensure_array_of_array(array) click to toggle source
# File lib/cucumber/core/ast/data_table.rb, line 102
def ensure_array_of_array(array)
  Hash === array[0] ? hashes_to_array(array) : array
end
hashes_to_array(hashes) click to toggle source
# File lib/cucumber/core/ast/data_table.rb, line 106
def hashes_to_array(hashes)
  header = hashes[0].keys.sort
  [header] + hashes.map{|hash| header.map{|key| hash[key]}}
end
verify_rows_are_same_length(raw) click to toggle source
# File lib/cucumber/core/ast/data_table.rb, line 94
def verify_rows_are_same_length(raw)
  begin
    raw.transpose
  rescue IndexError
    raise ArgumentError, "Rows must all be the same length"
  end
end