object_table 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -134,6 +134,40 @@ EOS
134
134
  EOS
135
135
  end
136
136
  end
137
+
138
+ context 'with > 3 dimensions in the matrix' do
139
+ let(:table){ ObjectTable.new(col1: 1..100, col2: NArray.int(2, 2, 100).indgen! ) }
140
+
141
+ it 'should align the columns' do
142
+ table = subject.inspect.split("\n")[1..-1].map(&:rstrip).join("\n") + "\n"
143
+ expect(table).to eql <<EOS
144
+ col1 col2
145
+ 0: 1 [ [ 0, 1 ],
146
+ [ 2, 3 ] ]
147
+ 1: 2 [ [ 4, 5 ],
148
+ [ 6, 7 ] ]
149
+ 2: 3 [ [ 8, 9 ],
150
+ [ 10, 11 ] ]
151
+ 3: 4 [ [ 12, 13 ],
152
+ [ 14, 15 ] ]
153
+ 4: 5 [ [ 16, 17 ],
154
+ [ 18, 19 ] ]
155
+ ------------------------------
156
+ 95: 96 [ [ 380, 381 ],
157
+ [ 382, 383 ] ]
158
+ 96: 97 [ [ 384, 385 ],
159
+ [ 386, 387 ] ]
160
+ 97: 98 [ [ 388, 389 ],
161
+ [ 390, 391 ] ]
162
+ 98: 99 [ [ 392, 393 ],
163
+ [ 394, 395 ] ]
164
+ 99: 100 [ [ 396, 397 ],
165
+ [ 398, 399 ] ]
166
+ col1 col2
167
+ EOS
168
+ end
169
+ end
170
+
137
171
  end
138
172
 
139
173
  context 'when raising a no method error' do
@@ -145,6 +179,7 @@ EOS
145
179
  end
146
180
  end
147
181
  end
182
+
148
183
  end
149
184
 
150
185
  describe '#nrows' do
@@ -305,7 +340,7 @@ EOS
305
340
  let(:grouped){ subject.group_by &block }
306
341
 
307
342
  it 'should return groups' do
308
- expect(grouped).to be_a ObjectTable::TempGrouped
343
+ expect(grouped).to be_a ObjectTable::Grouped
309
344
  expect(grouped.instance_eval('@grouper')).to eql block
310
345
  end
311
346
  end
@@ -170,4 +170,28 @@ RSpec.shared_examples 'a table view' do |cls|
170
170
  end
171
171
  end
172
172
 
173
+ describe '#inspect' do
174
+ context 'with an empty table' do
175
+ let(:table){ ObjectTable.new }
176
+ let(:block){ Proc.new{nil} }
177
+
178
+ subject{ table.where{ nil } }
179
+
180
+ it 'should say it is empty' do
181
+ text = subject.inspect.split("\n")[1..-1].map(&:rstrip).join("\n")
182
+ expect(text).to eql "(empty table)"
183
+ end
184
+ end
185
+
186
+ context 'with table with no rows' do
187
+ subject{ ObjectTable.new(col1: [], col2: []) }
188
+ let(:block){ Proc.new{nil} }
189
+
190
+ it 'should give the columns' do
191
+ text = subject.inspect.split("\n")[1..-1].map(&:rstrip).join("\n")
192
+ expect(text).to eql "(empty table with columns: col1, col2)"
193
+ end
194
+ end
195
+ end
196
+
173
197
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cheney Lin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-09 00:00:00.000000000 Z
11
+ date: 2015-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: narray
@@ -84,9 +84,10 @@ files:
84
84
  - lib/object_table/group.rb
85
85
  - lib/object_table/grouped.rb
86
86
  - lib/object_table/masked_column.rb
87
+ - lib/object_table/printable.rb
87
88
  - lib/object_table/static_view.rb
89
+ - lib/object_table/table_child.rb
88
90
  - lib/object_table/table_methods.rb
89
- - lib/object_table/temp_grouped.rb
90
91
  - lib/object_table/version.rb
91
92
  - lib/object_table/view.rb
92
93
  - lib/object_table/view_methods.rb
@@ -96,7 +97,6 @@ files:
96
97
  - spec/object_table/grouped_spec.rb
97
98
  - spec/object_table/masked_column_spec.rb
98
99
  - spec/object_table/static_view_spec.rb
99
- - spec/object_table/temp_grouped_spec.rb
100
100
  - spec/object_table/view_spec.rb
101
101
  - spec/object_table_spec.rb
102
102
  - spec/subclassing_spec.rb
@@ -132,7 +132,6 @@ test_files:
132
132
  - spec/object_table/grouped_spec.rb
133
133
  - spec/object_table/masked_column_spec.rb
134
134
  - spec/object_table/static_view_spec.rb
135
- - spec/object_table/temp_grouped_spec.rb
136
135
  - spec/object_table/view_spec.rb
137
136
  - spec/object_table_spec.rb
138
137
  - spec/subclassing_spec.rb
@@ -1,43 +0,0 @@
1
- require 'forwardable'
2
- require_relative 'grouped'
3
-
4
- class ObjectTable::TempGrouped
5
- extend Forwardable
6
- def_delegators :make_grouped, :each, :apply
7
-
8
- def initialize(parent, *names, &grouper)
9
- @parent = parent
10
- @grouper = grouper
11
- @names = names
12
- end
13
-
14
- def _groups
15
- names, keys = _keys()
16
- groups = (0...@parent.nrows).zip(keys).group_by{|row, key| key}
17
- groups.each do |k, v|
18
- groups[k] = NArray.to_na(v.map(&:first))
19
- end
20
- [names, groups]
21
- end
22
-
23
- def _keys
24
- if @names.empty?
25
- keys = @parent.apply(&@grouper)
26
- raise 'Group keys must be hashes' unless keys.is_a?(Hash)
27
- keys = ObjectTable::BasicGrid.new.replace keys
28
- else
29
- keys = ObjectTable::BasicGrid[@names.map{|n| [n, @parent.get_column(n)]}]
30
- end
31
-
32
- keys._ensure_uniform_columns!(@parent.nrows)
33
- names = keys.keys
34
- keys = keys.values.map(&:to_a).transpose
35
- [names, keys]
36
- end
37
-
38
- def make_grouped
39
- names, groups = _groups()
40
- ObjectTable::Grouped.new(@parent, names, groups)
41
- end
42
-
43
- end
@@ -1,143 +0,0 @@
1
- require 'object_table'
2
- require 'object_table/temp_grouped'
3
-
4
- describe ObjectTable::TempGrouped do
5
- let(:table){ ObjectTable.new(col1: [1, 2, 3, 4], col2: [5, 6, 7, 8] ) }
6
- # group based on parity (even vs odd)
7
- let(:grouped){ ObjectTable::TempGrouped.new(table){ {parity: col1 % 2} } }
8
-
9
- let(:even){ (table.col1 % 2).eq(0).where }
10
- let(:odd) { (table.col1 % 2).eq(1).where }
11
-
12
- describe '#initialize' do
13
-
14
- context 'when the block takes an argument' do
15
- it 'should not evaluate in the context of the table' do
16
- rspec_context = self
17
-
18
- grouped = ObjectTable::TempGrouped.new(table) do |tbl|
19
- receiver = eval('self', binding)
20
- expect(receiver).to_not be table
21
- expect(receiver).to be rspec_context
22
- {}
23
- end
24
- grouped._groups # call _groups to make it call the block
25
- end
26
-
27
- it 'should pass the table into the block' do
28
- grouped = ObjectTable::TempGrouped.new(table) do |tbl|
29
- expect(tbl).to be table
30
- {}
31
- end
32
- grouped._groups # call _groups to make it call the block
33
- end
34
- end
35
-
36
- context 'when the block takes no arguments' do
37
- it 'should call the block in the context of the table' do
38
- _ = self
39
- grouped = ObjectTable::TempGrouped.new(table) do
40
- receiver = eval('self', binding)
41
- _.expect(receiver).to _.be _.table
42
- {}
43
- end
44
- grouped._groups # call _groups to make it call the block
45
- end
46
- end
47
-
48
- end
49
-
50
- context 'with changes to the parent' do
51
- subject{ grouped }
52
-
53
- it 'should mirror changes to the parent' do
54
- expect(subject._groups[1]).to eql ({[0] => NArray[1, 3], [1] => NArray[0, 2]})
55
- table[:col1] = [2, 3, 4, 5]
56
- expect(subject._groups[1]).to eql ({[0] => NArray[0, 2], [1] => NArray[1, 3]})
57
- end
58
- end
59
-
60
- describe '#_groups' do
61
- subject{ grouped._groups }
62
-
63
- it 'should return the names' do
64
- expect(subject[0]).to eql [:parity]
65
- end
66
-
67
- it 'should return the group key => row mapping' do
68
- groups = subject[1]
69
- expect(groups[[0]]).to eql even
70
- expect(groups[[1]]).to eql odd
71
- end
72
-
73
- context 'when grouping by columns' do
74
- let(:table){ ObjectTable.new(key1: [0]*4 + [1]*4, key2: [0, 0, 1, 1]*2, data: 1..8 ) }
75
- let(:grouped){ ObjectTable::TempGrouped.new(table, :key1, :key2) }
76
-
77
- it 'should use the columns as group names' do
78
- expect(subject[0]).to eql [:key1, :key2]
79
- end
80
-
81
- it 'should use the columns as groups' do
82
- groups = subject[1]
83
- expect(groups[[0, 0]]).to eql (table.key1.eq(0) & table.key2.eq(0)).where
84
- expect(groups[[0, 1]]).to eql (table.key1.eq(0) & table.key2.eq(1)).where
85
- expect(groups[[1, 0]]).to eql (table.key1.eq(1) & table.key2.eq(0)).where
86
- expect(groups[[1, 1]]).to eql (table.key1.eq(1) & table.key2.eq(1)).where
87
- end
88
- end
89
- end
90
-
91
- describe '#each' do
92
- let(:table){ ObjectTable.new(col1: [1, 2, 3], col2: 5) }
93
- let(:block){ Proc.new{col1 + 100} }
94
-
95
- let(:names){ subject._groups[0] }
96
- let(:groups){ subject._groups[1] }
97
-
98
- subject{ ObjectTable::TempGrouped.new(table){ {parity: col1 % 2} } }
99
-
100
- it 'should create a group' do
101
- group = spy('group')
102
- expect(ObjectTable::Grouped).to receive(:new).with(table, names, groups){ group }
103
- subject.each(&block)
104
- end
105
-
106
- it 'should call #each on the view' do
107
- group = spy('group')
108
- expect(ObjectTable::Grouped).to receive(:new){ group }
109
- expect(group).to receive(:each) do |&b|
110
- expect(b).to be block
111
- end
112
-
113
- subject.each(&block)
114
- end
115
- end
116
-
117
- describe '#apply' do
118
- let(:table){ ObjectTable.new(col1: [1, 2, 3], col2: 5) }
119
- let(:block){ Proc.new{col1 + 100} }
120
-
121
- let(:names){ subject._groups[0] }
122
- let(:groups){ subject._groups[1] }
123
-
124
- subject{ ObjectTable::TempGrouped.new(table){ {parity: col1 % 2} } }
125
-
126
- it 'should create a group' do
127
- group = spy('group')
128
- expect(ObjectTable::Grouped).to receive(:new).with(table, names, groups){ group }
129
- subject.apply(&block)
130
- end
131
-
132
- it 'should call #apply on the view' do
133
- group = spy('group')
134
- expect(ObjectTable::Grouped).to receive(:new){ group }
135
- expect(group).to receive(:apply) do |&b|
136
- expect(b).to be block
137
- end
138
-
139
- subject.apply(&block)
140
- end
141
- end
142
-
143
- end