Class: Cucumber::Core::Ast::DataTable

Inherits:
Object
  • Object
show all
Includes:
DescribesItself, HasLocation, Gherkin::Rubify
Defined in:
lib/cucumber/core/ast/data_table.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasLocation

#attributes, #comments, #file, #file_colon_line, #line, #location, #match_locations?, #multiline_arg, #tags

Methods included from DescribesItself

#describe_to

Constructor Details

#initialize(raw, location) ⇒ DataTable

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.



40
41
42
43
44
45
# File 'lib/cucumber/core/ast/data_table.rb', line 40

def initialize(raw, location)
  raw = ensure_array_of_array(rubify(raw))
  verify_rows_are_same_length(raw)
  @raw = raw.freeze
  @location = location
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



46
47
48
# File 'lib/cucumber/core/ast/data_table.rb', line 46

def raw
  @raw
end

Instance Method Details

#==(other) ⇒ Object



89
90
91
# File 'lib/cucumber/core/ast/data_table.rb', line 89

def ==(other)
  other.class == self.class && raw == other.raw
end

#data_table?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/cucumber/core/ast/data_table.rb', line 52

def data_table?
  true
end

#doc_string?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/cucumber/core/ast/data_table.rb', line 56

def doc_string?
  false
end

#dupObject

Creates a copy of this table



62
63
64
# File 'lib/cucumber/core/ast/data_table.rb', line 62

def dup
  self.class.new(raw.dup, location)
end

#inspectObject



93
94
95
# File 'lib/cucumber/core/ast/data_table.rb', line 93

def inspect
  %{#<#{self.class} #{raw.inspect} (#{location})>}
end

#map(&block) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/cucumber/core/ast/data_table.rb', line 81

def map(&block)
  new_raw = raw.map do |row|
    row.map(&block)
  end

  self.class.new(new_raw, location)
end

#to_step_definition_argObject



48
49
50
# File 'lib/cucumber/core/ast/data_table.rb', line 48

def to_step_definition_arg
  dup
end

#transposeObject

Returns a new, transposed table. Example:

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

Gets converted into the following:

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



77
78
79
# File 'lib/cucumber/core/ast/data_table.rb', line 77

def transpose
  self.class.new(raw.transpose, location)
end