Skip to content

Commit

Permalink
Ast::Table can now be constructed with an Array of Hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Oct 18, 2009
1 parent 845bcc5 commit 9dbf98a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
3 changes: 3 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
== 0.4.3 (In Git)

=== New Features
* Ast::Table can now be constructed with an Array of Hash. (Aslak Hellesøy)

== 0.4.2 2009-10-14

Bugfix release. The 0.4.1 release was hosed when switching from Hoe to Jeweler.
Expand Down
12 changes: 9 additions & 3 deletions lib/cucumber/ast/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ def self.default_arg_name #:nodoc:
"table"
end

# Creates a new instance. +raw+ should be an Array of Array of String.
# Creates a new instance. +raw+ should be an Array of Array of String
# or an Array of Hash (similar to what #hashes returns).
# You don't typically create your own Table objects - Cucumber will do
# it internally and pass them to your Step Definitions.
#
def initialize(raw, conversion_procs = NULL_CONVERSIONS.dup)
@cells_class = Cells
@cell_class = Cell

raw = ensure_array_of_array(raw)
# Verify that it's square
transposed = raw.transpose
create_cell_matrix(raw)
Expand Down Expand Up @@ -524,11 +526,15 @@ def pad!(other_cell_matrix) #:nodoc:

def ensure_table(table_or_array) #:nodoc:
return table_or_array if Table === table_or_array
table_or_array = hashes_to_array(table_or_array) if Hash === table_or_array[0]
table_or_array = enumerable_to_array(table_or_array) unless Array == table_or_array[0]
Table.new(table_or_array)
end

def ensure_array_of_array(array)
return hashes_to_array(array) if Hash === array[0]
return enumerable_to_array(array) unless Array == array[0]
array
end

def hashes_to_array(hashes) #:nodoc:
header = hashes[0].keys
[header] + hashes.map{|hash| header.map{|key| hash[key]}}
Expand Down
12 changes: 11 additions & 1 deletion spec/cucumber/ast/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,17 @@ def table(text, file=nil, line_offset=0)
@table_parser.parse_or_fail(text.strip, file, line_offset)
end
end


describe "#new" do
it "should allow Array of Hash" do
t1 = Table.new([{'name' => 'aslak', 'male' => 'true'}])
t1.to_s(:indent => 12, :color => false).should == %{
| name | male |
| aslak | true |
}
end
end

it "should convert to sexp" do
@table.to_sexp.should ==
[:table,
Expand Down

0 comments on commit 9dbf98a

Please sign in to comment.