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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Table

Returns a new instance of Table.



13
14
15
16
17
18
19
# File 'lib/cucumber/ast/table.rb', line 13

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

Instance Attribute Details

#fileObject

Returns the value of attribute file.



11
12
13
# File 'lib/cucumber/ast/table.rb', line 11

def file
  @file
end

Instance Method Details

#accept(visitor, status) ⇒ Object



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

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

#arguments_replaced(arguments) ⇒ Object

:nodoc:



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cucumber/ast/table.rb', line 86

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 = cell_with_replaced_args.gsub(name, value)
      end
      cell_with_replaced_args
    end
  end

  Table.new(raw_with_replaced_args)
end

#at_lines?(lines) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/cucumber/ast/table.rb', line 21

def at_lines?(lines)
  rows.detect { |row| row.at_lines?(lines) }
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'}]


44
45
46
47
48
# File 'lib/cucumber/ast/table.rb', line 44

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

#index(cells) ⇒ Object

:nodoc:



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

def index(cells) #:nodoc:
  rows.index(cells)
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']]


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

def raw
  @raw
end

#to_hash(cells) ⇒ Object

:nodoc:



74
75
76
77
78
79
80
# File 'lib/cucumber/ast/table.rb', line 74

def to_hash(cells) #:nodoc:
  hash = {}
  @raw[0].each_with_index do |key, n|
    hash[key] = cells.value(n)
  end
  hash
end

#to_sexpObject

For testing only



70
71
72
# File 'lib/cucumber/ast/table.rb', line 70

def to_sexp #:nodoc:
  [:table, *rows.map{|row| row.to_sexp}]
end