Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

refactoring: convert the row-hash into a strcut

  • Loading branch information...
commit a0cd3ccb82e806ddc36e1271803d661cadfb97fa 1 parent 4e00f31
Yves Senn authored September 16, 2012
11  lib/corner_stones/table.rb
@@ -18,7 +18,7 @@ def initialize(scope, options = {})
18 18
 
19 19
     def row(options)
20 20
       rows.detect { |row|
21  
-        identity = row.select { |key, value| options.has_key?(key) }
  21
+        identity = row.attributes.select { |key, value| options.has_key?(key) }
22 22
         identity == options
23 23
       } or raise MissingRowError, "no row with '#{options.inspect}'\n\ngot:#{rows}"
24 24
     end
@@ -26,7 +26,7 @@ def row(options)
26 26
     def rows
27 27
       within @scope do
28 28
         all('tbody tr').map do |row|
29  
-          attributes_for_row(row)
  29
+          row = Row.new(row, attributes_for_row(row))
30 30
         end
31 31
       end
32 32
     end
@@ -44,7 +44,6 @@ def attributes_for_row(row)
44 44
       headers.each.with_index.with_object(row_data) do |(header, index), row_data|
45 45
         augment_row_with_cell(row_data, row, index, header)
46 46
       end
47  
-      row_data['Row-Element'] = row
48 47
       row_data
49 48
     end
50 49
 
@@ -57,6 +56,12 @@ def augment_row_with_cell(row_data, row, index, header)
57 56
     def value_for_cell(cell)
58 57
       cell.text unless cell.nil?
59 58
     end
  59
+
  60
+    Row = Struct.new(:node, :attributes) do
  61
+      def [](key)
  62
+        attributes[key]
  63
+      end
  64
+    end
60 65
   end
61 66
 
62 67
 end
2  spec/integration/corner_stones/table_form_spec.rb
@@ -139,7 +139,7 @@
139 139
     it 'ignores empty cells' do
140 140
       expected_data = [{'Title' => 'Indiana Jones', 'Duration' => '210 minutes', 'Time' => nil}]
141 141
       subject.rows.map {|r|
142  
-        r.reject {|key, value| ['Row-Element', 'Inputs'].include? key}
  142
+        r.attributes.reject {|key, _value| !expected_data.first.has_key?(key)}
143 143
       }.must_equal(expected_data)
144 144
     end
145 145
   end
27  spec/integration/corner_stones/table_spec.rb
@@ -51,26 +51,20 @@
51 51
                        { 'ID' => '2', 'Title' => 'Domain Driven Design', 'Author' => 'Eric Evans'}]
52 52
 
53 53
       subject.rows.map {|r|
54  
-        r.each do |k, v|
55  
-          r.delete(k) unless expected_data.first.has_key?(k)
  54
+        r.attributes.reject do |key, _value|
  55
+          !expected_data.first.has_key?(key)
56 56
         end
57 57
       }.must_equal(expected_data)
58 58
     end
59 59
 
60 60
     it 'a row can be accessed with a single key' do
61  
-      expected_data = { 'ID' => '2', 'Title' => 'Domain Driven Design', 'Author' => 'Eric Evans' }
62 61
       actual = subject.row('Title' => 'Domain Driven Design')
63  
-
64  
-      actual.each {|k, v| actual.delete(k) unless expected_data.has_key?(k)}
65  
-      actual.must_equal(expected_data)
  62
+      actual['Author'].must_equal('Eric Evans')
66 63
     end
67 64
 
68 65
     it 'a row can be accessed with multiple keys' do
69  
-      expected_data = {'ID' => '1', 'Title' => 'Clean Code', 'Author' => 'Robert C. Martin'}
70  
-
71 66
       actual = subject.row('ID' => '1', 'Author' => 'Robert C. Martin')
72  
-      actual.each {|k, v| actual.delete(k) unless expected_data.has_key?(k)}
73  
-      actual.must_equal(expected_data)
  67
+      actual['Title'].must_equal('Clean Code')
74 68
     end
75 69
 
76 70
     it 'It raises an Exception when no Row was found' do
@@ -80,7 +74,7 @@
80 74
     end
81 75
 
82 76
     it 'extracts the Capybara-Element for the table row' do
83  
-      subject.row('ID' => '1')['Row-Element'].path.must_equal('/html/body/table/tbody/tr[1]')
  77
+      subject.row('ID' => '1').node.path.must_equal('/html/body/table/tbody/tr[1]')
84 78
     end
85 79
   end
86 80
 
@@ -110,8 +104,8 @@
110 104
                          { 'Book' => 'Domain Driven Design',
111 105
                            'Author' => 'Eric Evans'}]
112 106
         subject.rows.map {|r|
113  
-          r.each do |k, v|
114  
-            r.delete(k) unless expected_data.first.has_key?(k)
  107
+          r.attributes.reject do |key, _value|
  108
+            !expected_data.first.has_key?(key)
115 109
           end
116 110
         }.must_equal(expected_data)
117 111
       end
@@ -141,9 +135,10 @@
141 135
 
142 136
     it 'ignores empty cells' do
143 137
       expected_data = [{'ID' => '1', 'Title' => 'Clean Code', 'Author' => nil}]
144  
-      subject.rows.map {|r|
145  
-        r.reject {|key, value| key == 'Row-Element'}
146  
-      }.must_equal(expected_data)
  138
+      actual = subject.rows
  139
+
  140
+      actual = actual.map {|row| row.attributes.reject {|key, _value| !expected_data.first.has_key?(key)}}
  141
+      actual.must_equal(expected_data)
147 142
     end
148 143
   end
149 144
 

0 notes on commit a0cd3cc

Please sign in to comment.
Something went wrong with that request. Please try again.