Permalink
Browse files

refactoring: convert the row-hash into a strcut

  • Loading branch information...
1 parent 4e00f31 commit a0cd3ccb82e806ddc36e1271803d661cadfb97fa @senny committed Sep 16, 2012
View
11 lib/corner_stones/table.rb
@@ -18,15 +18,15 @@ def initialize(scope, options = {})
def row(options)
rows.detect { |row|
- identity = row.select { |key, value| options.has_key?(key) }
+ identity = row.attributes.select { |key, value| options.has_key?(key) }
identity == options
} or raise MissingRowError, "no row with '#{options.inspect}'\n\ngot:#{rows}"
end
def rows
within @scope do
all('tbody tr').map do |row|
- attributes_for_row(row)
+ row = Row.new(row, attributes_for_row(row))
end
end
end
@@ -44,7 +44,6 @@ def attributes_for_row(row)
headers.each.with_index.with_object(row_data) do |(header, index), row_data|
augment_row_with_cell(row_data, row, index, header)
end
- row_data['Row-Element'] = row
row_data
end
@@ -57,6 +56,12 @@ def augment_row_with_cell(row_data, row, index, header)
def value_for_cell(cell)
cell.text unless cell.nil?
end
+
+ Row = Struct.new(:node, :attributes) do
+ def [](key)
+ attributes[key]
+ end
+ end
end
end
View
2 spec/integration/corner_stones/table_form_spec.rb
@@ -139,7 +139,7 @@
it 'ignores empty cells' do
expected_data = [{'Title' => 'Indiana Jones', 'Duration' => '210 minutes', 'Time' => nil}]
subject.rows.map {|r|
- r.reject {|key, value| ['Row-Element', 'Inputs'].include? key}
+ r.attributes.reject {|key, _value| !expected_data.first.has_key?(key)}
}.must_equal(expected_data)
end
end
View
27 spec/integration/corner_stones/table_spec.rb
@@ -51,26 +51,20 @@
{ 'ID' => '2', 'Title' => 'Domain Driven Design', 'Author' => 'Eric Evans'}]
subject.rows.map {|r|
- r.each do |k, v|
- r.delete(k) unless expected_data.first.has_key?(k)
+ r.attributes.reject do |key, _value|
+ !expected_data.first.has_key?(key)
end
}.must_equal(expected_data)
end
it 'a row can be accessed with a single key' do
- expected_data = { 'ID' => '2', 'Title' => 'Domain Driven Design', 'Author' => 'Eric Evans' }
actual = subject.row('Title' => 'Domain Driven Design')
-
- actual.each {|k, v| actual.delete(k) unless expected_data.has_key?(k)}
- actual.must_equal(expected_data)
+ actual['Author'].must_equal('Eric Evans')
end
it 'a row can be accessed with multiple keys' do
- expected_data = {'ID' => '1', 'Title' => 'Clean Code', 'Author' => 'Robert C. Martin'}
-
actual = subject.row('ID' => '1', 'Author' => 'Robert C. Martin')
- actual.each {|k, v| actual.delete(k) unless expected_data.has_key?(k)}
- actual.must_equal(expected_data)
+ actual['Title'].must_equal('Clean Code')
end
it 'It raises an Exception when no Row was found' do
@@ -80,7 +74,7 @@
end
it 'extracts the Capybara-Element for the table row' do
- subject.row('ID' => '1')['Row-Element'].path.must_equal('/html/body/table/tbody/tr[1]')
+ subject.row('ID' => '1').node.path.must_equal('/html/body/table/tbody/tr[1]')
end
end
@@ -110,8 +104,8 @@
{ 'Book' => 'Domain Driven Design',
'Author' => 'Eric Evans'}]
subject.rows.map {|r|
- r.each do |k, v|
- r.delete(k) unless expected_data.first.has_key?(k)
+ r.attributes.reject do |key, _value|
+ !expected_data.first.has_key?(key)
end
}.must_equal(expected_data)
end
@@ -141,9 +135,10 @@
it 'ignores empty cells' do
expected_data = [{'ID' => '1', 'Title' => 'Clean Code', 'Author' => nil}]
- subject.rows.map {|r|
- r.reject {|key, value| key == 'Row-Element'}
- }.must_equal(expected_data)
+ actual = subject.rows
+
+ actual = actual.map {|row| row.attributes.reject {|key, _value| !expected_data.first.has_key?(key)}}
+ actual.must_equal(expected_data)
end
end

0 comments on commit a0cd3cc

Please sign in to comment.