Skip to content

Commit

Permalink
Merge b40d6ea into c95539e
Browse files Browse the repository at this point in the history
  • Loading branch information
dmorgan3405 committed Apr 21, 2015
2 parents c95539e + b40d6ea commit 7aa0ffd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/cucumber/multiline_argument/data_table.rb
Expand Up @@ -148,6 +148,22 @@ def hashes
@hashes ||= build_hashes
end

# Converts this table into an Array of Hashes where the keys are symbols.
# For example, a Table built from the following plain text:
#
# | foo | Bar | Foo Bar |
# | 2 | 3 | 5 |
# | 7 | 9 | 16 |
#
# Gets converted into the following:
#
# [{:foo => '2', :bar => '3', :foo_bar => '5'}, {:foo => '7', :bar => '9', :foo_bar => '16'}]
#
def symbolic_hashes
@header_conversion_proc = lambda { |h| symbolize_key(h) }
@symbolic_hashes ||= build_hashes
end

# Converts this table into a Hash where the first column is
# used as keys and the second column is used as values
#
Expand Down Expand Up @@ -616,6 +632,10 @@ def mark_as_missing(col) #:nodoc:
end
end

def symbolize_key(key)
key.downcase.tr(' ', '_').to_sym
end

# Represents a row of cells or columns of cells
class Cells #:nodoc:
include Enumerable
Expand Down
15 changes: 15 additions & 0 deletions spec/cucumber/multiline_argument/data_table_spec.rb
Expand Up @@ -39,6 +39,21 @@ module MultilineArgument
expect( @table.rows.first ).to eq %w{4444 55555 666666}
end

describe '#symbolic_hashes' do

it 'should covert data table to an array of hashes with symbols as keys' do
ast_table = Cucumber::Core::Ast::DataTable.new([['foo', 'Bar', 'Foo Bar'], %w{1 22 333}], nil)
data_table = DataTable.new(ast_table)
expect(data_table.symbolic_hashes).to eq([{:foo => '1', :bar => '22', :foo_bar => '333'}])
end

it 'should be able to be accessed multiple times' do
@table.symbolic_hashes
expect{@table.symbolic_hashes}.to_not raise_error
end

end

describe '#map_column!' do
it "should allow mapping columns" do
@table.map_column!('one') { |v| v.to_i }
Expand Down

0 comments on commit 7aa0ffd

Please sign in to comment.