Class: Cucumber::Ast::Table

Inherits:
Object show all
Defined in:
lib/cucumber/ast/table.rb

Overview

Holds the data of a table parsed from a feature file:

| a | b |
| c | d |

This gets parsed into a Table holding the values [['a', 'b'], ['c', 'd']]

Direct Known Subclasses

OutlineTable

Defined Under Namespace

Classes: Cell, Cells

Constant Summary collapse

NULL_CONVERSIONS =
Hash.new(lambda{ |cell_value| cell_value }).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, conversions = NULL_CONVERSIONS.dup) ⇒ Table

Returns a new instance of Table.



15
16
17
18
19
20
21
22
# File 'lib/cucumber/ast/table.rb', line 15

def initialize(raw, conversions = NULL_CONVERSIONS.dup)
  # Verify that it's square
  raw.transpose
  @raw = raw
  @cells_class = Cells
  @cell_class = Cell
  @conversion_procs = conversions
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



13
14
15
# File 'lib/cucumber/ast/table.rb', line 13

def file
  @file
end

Instance Method Details

#accept(visitor, status) ⇒ Object



91
92
93
94
95
96
# File 'lib/cucumber/ast/table.rb', line 91

def accept(visitor, status)
  cells_rows.each do |row|
    visitor.visit_table_row(row, status)
  end
  nil
end

#arguments_replaced(arguments) ⇒ Object

:nodoc:



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cucumber/ast/table.rb', line 150

def arguments_replaced(arguments) #:nodoc:
  raw_with_replaced_args = raw.map do |row|
    row.map do |cell|
      cell_with_replaced_args = cell
      arguments.each do |name, value|
        cell_with_replaced_args = value && !cell.nil? ? cell_with_replaced_args.gsub(name, value) : nil
      end
      cell_with_replaced_args
    end
  end

  Table.new(raw_with_replaced_args)
end

#at_lines?(lines) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/cucumber/ast/table.rb', line 87

def at_lines?(lines)
  cells_rows.detect { |row| row.at_lines?(lines) }
end

#dupObject

Creates a copy of this table, inheriting the column mappings.



25
26
27
# File 'lib/cucumber/ast/table.rb', line 25

def dup
  self.class.new(@raw.dup, @conversion_procs.dup)
end

#each_cells_row(&proc) ⇒ Object



83
84
85
# File 'lib/cucumber/ast/table.rb', line 83

def each_cells_row(&proc)
  cells_rows.each(&proc)
end

#hashesObject

Converts this table into an Array of Hash where the keys of each Hash are the headers in the table. For example, a Table built from the following plain text:

| a | b | sum |
| 2 | 3 | 5   |
| 7 | 9 | 16  |

Gets converted into the following:

[{'a' => '2', 'b' => '3', 'sum' => '5'}, {'a' => '7', 'b' => '9', 'sum' => '16'}]

Use #map_column! to specify how values in a column are converted.



58
59
60
61
62
# File 'lib/cucumber/ast/table.rb', line 58

def hashes
  @hashes ||= cells_rows[1..-1].map do |row|
    row.to_hash
  end
end

#index(cells) ⇒ Object

:nodoc:



142
143
144
# File 'lib/cucumber/ast/table.rb', line 142

def index(cells) #:nodoc:
  cells_rows.index(cells)
end

#map_column!(column_name, strict = true, &conversion_proc) ⇒ Object

Change how #hashes converts column values. The column_name argument identifies the column and conversion_proc performs the conversion for each cell in that column. If strict is true, an error will be raised if the column named column_name is not found. If strict is false, no error will be raised.



126
127
128
129
# File 'lib/cucumber/ast/table.rb', line 126

def map_column!(column_name, strict=true, &conversion_proc)
  verify_column(column_name) if strict
  @conversion_procs[column_name] = conversion_proc
end

#map_headers(mappings) ⇒ Object

Returns a new Table where the headers are redefined. This makes it possible to use prettier header names in the features. Example:

| Phone Number | Address |
| 123456       | xyz     |
| 345678       | abc     |

A StepDefinition receiving this table can then map the columns:

mapped_table = table.map_columns('Phone Number' => :phone, 'Address' => :address)
hashes = mapped_table.hashes
# => [{:phone => '123456', :address => 'xyz'}, {:phone => '345678', :address => 'abc'}]


116
117
118
119
120
# File 'lib/cucumber/ast/table.rb', line 116

def map_headers(mappings)
  table = self.dup
  table.map_headers!(mappings)
  table
end

#rawObject

Gets the raw data of this table. For example, a Table built from the following plain text:

| a | b |
| c | d |

Get converted into the following:

[['a', 'b], ['c', 'd']]


74
75
76
# File 'lib/cucumber/ast/table.rb', line 74

def raw
  @raw
end

#rowsObject

Same as #raw, but skips the first (header) row



79
80
81
# File 'lib/cucumber/ast/table.rb', line 79

def rows
  @raw[1..-1]
end

#to_hash(cells) ⇒ Object

:nodoc:



131
132
133
134
135
136
137
138
139
140
# File 'lib/cucumber/ast/table.rb', line 131

def to_hash(cells) #:nodoc:
  hash = Hash.new do |hash, key|
    hash[key.to_s] if key.is_a?(Symbol)
  end
  @raw[0].each_with_index do |column_name, column_index|
    value = @conversion_procs[column_name].call(cells.value(column_index))
    hash[column_name] = value
  end
  hash
end

#to_sexpObject

For testing only



99
100
101
# File 'lib/cucumber/ast/table.rb', line 99

def to_sexp #:nodoc:
  [:table, *cells_rows.map{|row| row.to_sexp}]
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 |



40
41
42
# File 'lib/cucumber/ast/table.rb', line 40

def transpose
  self.class.new(@raw.transpose, @conversion_procs.dup)
end

#verify_column(column_name) ⇒ Object



146
147
148
# File 'lib/cucumber/ast/table.rb', line 146

def verify_column(column_name)
  raise %{The column named "#{column_name}" does not exist} unless @raw[0].include?(column_name)
end