tablestakes 0.8.4 → 0.8.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tablestakes.rb +79 -34
  3. data/spec/table_spec.rb +105 -3
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c4827f9fe7583ab475c276cb8930937e51e7c42
4
- data.tar.gz: f27a3196f816a096c8d311e3288d4ab3126005dd
3
+ metadata.gz: f98f7f613b50b179880e3994802ee92e8f95592d
4
+ data.tar.gz: 7b68027ba7ad45b2b010db95eebcee1d5d4dedf2
5
5
  SHA512:
6
- metadata.gz: f3539e569ca9534174250d0f0a697443e3bbc2e8fa3baf19d6d4ea4787f06504b4150b8bdaf0145daf33d1ce77e5b34b0e1b4de25c312852a0b4d22e94e88206
7
- data.tar.gz: 6513f7ef2d5ef1dbb3a696b57dae6915ce018f0a6d274b4b1572cd51192df67fd0e92e5cea0e3519339a8e97dccce068207a448d2402283d1f8890af69328990
6
+ metadata.gz: 08ceccfccfabab0f825192ed075124e1a1a6e301183de4060fb16f9b19fb7ba9d47be29888fe8033bd0c99404356fd4c88571e0f67016987021d008dc88812b7
7
+ data.tar.gz: 5a9f004ff941df3caf43f7a533fcda9db1c2444f34b4a517694817d285c206bb523a8a675f892234d1e918d514bf43b73b74793b16eff5e7e88fd328010555d0
data/lib/tablestakes.rb CHANGED
@@ -66,52 +66,102 @@ class Table
66
66
  def row(index)
67
67
  Array(get_row(index))
68
68
  end
69
+
70
+ # Return true if the Table is empty, false otherwise.
71
+ #
72
+ def empty?
73
+ @headers.length == 0 && @table.length == 0
74
+ end
69
75
 
70
- # Add a column to the Table. Returns nil if the column name is already taken
76
+ # Add a column to the Table. Raises ArgumentError if the column name is already taken
71
77
  # or there are not the correct number of values.
72
78
  #
73
79
  # +colname+:: +String+ to identify the name of the column
74
80
  # +column_vals+:: +Array+ to hold the column values
75
- def add_column(colname, column_vals)
81
+ # Examples:
82
+ # add_column("Header", [e1, e2, e3])
83
+ # add_column(array_including_header)
84
+ # add_column("Header", "e1", "e2", ...)
85
+ def add_column(*args)
86
+ if args.kind_of? Array
87
+ args = args.flatten
88
+ colname = args.shift
89
+ column_vals = args
90
+ else
91
+ raise ArgumentError, "Invalid Arguments to add_column"
92
+ end
76
93
  # check arguments
77
- return nil if @table.has_key?(colname)
78
- return nil unless column_vals.length == @table[@headers.first].length
94
+ raise ArgumentError, "Duplicate Column Name!" if @table.has_key?(colname)
95
+ unless self.empty?
96
+ if column_vals.length != @table[@headers.first].length
97
+ raise ArgumentError, "Number of elements in column does not match existing table"
98
+ end
99
+ end
100
+ append_col(colname, column_vals)
101
+ end
79
102
 
80
- @headers << colname
81
- @table[colname] = Array.new(column_vals)
103
+ # Add one or more rows to the Table, appending it to the end. Raises ArgumentError if
104
+ # there are not the correct number of values. The first row becomes the table headers
105
+ # if currently undefined.
106
+ #
107
+ # +array_of_rows+:: +Array+ of +Arrays+ to hold the rows values
108
+ # Examples:
109
+ # add_rows([ [e1, e2, e3], [e1, e2, e3] ])
110
+ def add_rows(array_of_rows)
111
+ array_of_rows.each do |r|
112
+ add_row(r.clone)
113
+ end
114
+ return self
82
115
  end
83
116
 
84
- # Add a row to the Table, appending it to the end. Returns nil if
117
+ # Add a row to the Table, appending it to the end. Raises ArgumentError if
85
118
  # there are not the correct number of values.
86
119
  #
87
- # +row_vals+:: +Array+ to hold the row values
88
- def add_row(row_vals)
89
- add_rows([row_vals])
120
+ # +row+:: +Array+ to hold the row values
121
+ # Examples:
122
+ # add_row([e1, e2, e3])
123
+ # add_row("e1", "e2", "e3", ...)
124
+ def add_row(*row)
125
+ if row.kind_of? Array
126
+ row = row.flatten
127
+ else
128
+ raise ArgumentError, "Invalid Arguments to add_row"
129
+ end
130
+ if @headers.empty?
131
+ @headers = row
132
+ else
133
+ unless row.length == @headers.length
134
+ raise ArgumentError, "Wrong number of fields in Table input"
135
+ end
136
+ append_row(row)
137
+ end
138
+ return self
90
139
  end
91
140
 
92
- # Delete a column from the Table. Returns nil if the column name does not exist.
141
+ # Delete a column from the Table. Raises ArgumentError if the column name does not exist.
93
142
  #
94
143
  # +colname+:: +String+ to identify the name of the column
95
144
  def del_column(colname)
96
145
  # check arguments
97
- return nil unless @table.has_key?(colname)
146
+ raise ArgumentError, "Column name does not exist!" unless @table.has_key?(colname)
98
147
 
99
148
  @headers.delete(colname)
100
149
  @table.delete(colname)
150
+ return self
101
151
  end
102
152
 
103
- # Delete a row from the Table. Returns nil if
153
+ # Delete a row from the Table. Raises ArgumentError if
104
154
  # the row number is not found.
105
155
  #
106
156
  # +rownum+:: +FixNum+ to hold the row number
107
157
  def del_row(rownum)
108
158
  # check arguments
109
- return nil unless rownum <= @table[@headers.first].length
159
+ raise ArgumentError, "Row number does not exist!" unless rownum <= @table[@headers.first].length
110
160
 
111
161
  @headers.each do |col|
112
162
  @table[col].delete_at(rownum)
113
163
  end
114
-
164
+ return self
115
165
  end
116
166
 
117
167
 
@@ -402,23 +452,6 @@ class Table
402
452
  end
403
453
  end
404
454
 
405
- def add_rows(array_of_rows)
406
- array_of_rows.each do |r|
407
- row = r.clone
408
- if @headers.empty?
409
- @headers = row
410
- else
411
- unless row.length == @headers.length
412
- raise ArgumentError, "Wrong number of fields in Table input"
413
- end
414
- @headers.each do |col|
415
- @table[col] = [] unless @table[col]
416
- @table[col] << row.shift
417
- end
418
- end
419
- end
420
- end
421
-
422
455
  def get_row(index)
423
456
  result = []
424
457
  @headers.each do |col|
@@ -427,11 +460,23 @@ class Table
427
460
  return result
428
461
  end
429
462
 
463
+ def append_row(row)
464
+ @headers.each do |col|
465
+ @table[col] = [] unless @table[col]
466
+ @table[col] << row.shift
467
+ end
468
+ end
469
+
430
470
  def get_col(colname)
431
- result = []
432
- @table[colname].each {|e| result << e }
471
+ Array.new(@table[colname])
433
472
  end
434
473
 
474
+ def append_col(colname, column_vals)
475
+ @headers << colname
476
+ @table[colname] = Array.new(column_vals)
477
+ return self
478
+ end
479
+
435
480
  def copy
436
481
  result = []
437
482
  result << @headers
data/spec/table_spec.rb CHANGED
@@ -9,13 +9,13 @@ describe "Table" do
9
9
 
10
10
  describe ".new" do
11
11
  let(:t) { Table.new('test.tab') }
12
- let(:s) { Table.new() }
12
+ let(:empty) { Table.new() }
13
13
 
14
14
  it "reads a file and create a table" do
15
15
  expect(t).to be_a(Table)
16
16
  end
17
17
  it "creates a table if no file was given" do
18
- expect(s).to be_a(Table)
18
+ expect(empty).to be_a(Table)
19
19
  end
20
20
  it "errors when the file is not found" do
21
21
  expect{Table.new('sillyfile.txt')}.to raise_error(Errno::ENOENT)
@@ -23,6 +23,108 @@ describe "Table" do
23
23
 
24
24
  end
25
25
 
26
+ describe ".empty?" do
27
+ let(:test) { Table.new('test.tab') }
28
+ let(:empty) { Table.new() }
29
+
30
+ it "recognizes an empty table" do
31
+ expect(empty).to be_empty()
32
+ end
33
+ it "recognizes a populated table" do
34
+ expect(test).not_to be_empty()
35
+ end
36
+ end
37
+
38
+ describe ".add_column" do
39
+ let(:test) { FactoryGirl.build(:table) }
40
+ let(:newcol) { ["A", "B", "C"] }
41
+ let(:headercol) { ["TestCol", "A", "B", "C"] }
42
+ let(:empty) { Table.new() }
43
+
44
+ it "returns a Table" do
45
+ expect(test.add_column("TestCol", newcol)).to be_a(Table)
46
+ end
47
+ it "adds a column to an empty table" do
48
+ expect(empty.add_column("TestCol", newcol).headers[0]).to eq("TestCol")
49
+ end
50
+ it "adds a column to a populated table" do
51
+ expect(test.add_column("TestCol", newcol).headers).to include("TestCol")
52
+ end
53
+ it "raises an ArgumentError when given a duplicate header" do
54
+ expect { test.add_column("Name", newcol) }.to raise_error(ArgumentError)
55
+ end
56
+ it "raises an ArgumentError when given a column with the wrong length" do
57
+ expect { test.add_column("Name", newcol << "D") }.to raise_error(ArgumentError)
58
+ end
59
+ it "adds a column when given an Array" do
60
+ expect(test.add_column(headercol).headers).to include("TestCol")
61
+ end
62
+ end
63
+
64
+ describe ".del_column" do
65
+ let(:test) { FactoryGirl.build(:table) }
66
+ let(:empty) { Table.new() }
67
+ let(:remaining_headers) { ["Address", "Phone", "Records"] }
68
+
69
+ it "returns a Table" do
70
+ expect(test.del_column("Name")).to be_a(Table)
71
+ end
72
+ it "removes the correct column from the Table" do
73
+ expect(test.del_column("Name").headers).not_to include("Name")
74
+ end
75
+ it "retains the other columns from the Table" do
76
+ expect(test.del_column("Name").headers - remaining_headers).to be_empty
77
+ end
78
+ it "raises an ArgumentError when given an invalid header" do
79
+ expect { test.del_column("Silly") }.to raise_error(ArgumentError)
80
+ end
81
+ end
82
+
83
+ describe ".add_row(s)" do
84
+ let(:test) { FactoryGirl.build(:table) }
85
+ let(:empty) { Table.new() }
86
+ let(:newheaders) { ["Name", "Address", "Phone", "Records"]}
87
+ let(:newrow) { ["Phil", "567 Vine", "567-432-1234", "3"] }
88
+ let(:newrow2) { ["Harry", "57 Maple", "567-555-4321", "2"] }
89
+
90
+ it "returns a Table" do
91
+ expect(test.add_row(newrow)).to be_a(Table)
92
+ end
93
+ it "adds a row to a populated table" do
94
+ expect(test.add_row(newrow).count).to eq(4)
95
+ end
96
+ it "adds headers to an empty table" do
97
+ expect(test.add_row(newheaders).headers.length).to eq(4)
98
+ end
99
+ it "raises an ArgumentError when given a row with the wrong length" do
100
+ expect { test.add_row(newrow << "extra") }.to raise_error(ArgumentError)
101
+ end
102
+ it "adds a header and row to an empty table" do
103
+ expect(empty.add_rows([newheaders] << newrow).headers.length).to eq(4)
104
+ end
105
+ it "adds row elements with specified headers to a populated table" do
106
+ expect(test.add_rows([newrow] << newrow2).count).to eq(5)
107
+ end
108
+ end
109
+
110
+ describe ".del_row" do
111
+ let(:test) { FactoryGirl.build(:table) }
112
+ let(:empty) { Table.new() }
113
+
114
+ it "returns a Table" do
115
+ expect(test.del_row(0)).to be_a(Table)
116
+ end
117
+ it "removes the correct row from the Table" do
118
+ expect(test.del_row(0).column("Name")).not_to include("John")
119
+ end
120
+ it "retains the other rows from the Table" do
121
+ expect(test.del_row(0).column("Name")).to include("Jerry")
122
+ end
123
+ it "raises an ArgumentError when given an index that is out of bounds" do
124
+ expect { test.del_row(10) }.to raise_error(ArgumentError)
125
+ end
126
+ end
127
+
26
128
  describe ".count" do
27
129
  let(:t) { FactoryGirl.build(:table) }
28
130
 
@@ -159,7 +261,7 @@ describe "Table" do
159
261
  end
160
262
 
161
263
  describe ".intersect" do
162
- let (:cities) { Table.new('cities.txt') }
264
+ let(:cities) { Table.new('cities.txt') }
163
265
  let(:capitals) { Table.new('capitals.txt') }
164
266
 
165
267
  it "returns an instance of Array" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tablestakes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.4
4
+ version: 0.8.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - J.B. Folkerts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-03 00:00:00.000000000 Z
11
+ date: 2014-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec