Skip to content

Commit

Permalink
refactoring: convert the row-hash into a strcut
Browse files Browse the repository at this point in the history
  • Loading branch information
senny committed Sep 16, 2012
1 parent 4e00f31 commit a0cd3cc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
11 changes: 8 additions & 3 deletions lib/corner_stones/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
2 changes: 1 addition & 1 deletion spec/integration/corner_stones/table_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 11 additions & 16 deletions spec/integration/corner_stones/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit a0cd3cc

Please sign in to comment.