chair 1.1.0 → 1.1.1
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.
- checksums.yaml +4 -4
- data/lib/chair/chair.rb +23 -6
- data/lib/chair/version.rb +1 -1
- data/spec/table_search_spec.rb +1 -1
- data/spec/table_spec.rb +22 -14
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 708b6c777ef65da883a84fea3b82efc0fab65d04
|
4
|
+
data.tar.gz: 6487ea6b33e0a3580ed3dee015e3dee76ef84bf0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad6141ebfb731b5c310b0f6f3180ad84881418de08e5d5b78d928aa3702c9cdbeacdc39142443cfc7696c74d61bf587be10a37d74e9ee0f24f5e0b9db84b10a0
|
7
|
+
data.tar.gz: f53d8948344d3dff2afa298c0501b74084590d03301720c94d87ef49e66d0e933380b3c37858bcaf7a73a6c7b494fe508e467c0e9c3b39d2ebef45f6682db355
|
data/lib/chair/chair.rb
CHANGED
@@ -4,7 +4,7 @@ require 'set'
|
|
4
4
|
# @attr_reader primary_key [Symbol] the primary key of the table
|
5
5
|
# @attr_reader indices [Set<Symbol>] the set of indices for the table
|
6
6
|
class Chair
|
7
|
-
attr_reader :primary_key
|
7
|
+
attr_reader :primary_key
|
8
8
|
|
9
9
|
# Creates a new Table object
|
10
10
|
# @param columns [Symbol] columns to insert into the table at initialization
|
@@ -118,6 +118,10 @@ class Chair
|
|
118
118
|
|
119
119
|
alias_method :count, :size
|
120
120
|
|
121
|
+
def indices
|
122
|
+
@indices.keys
|
123
|
+
end
|
124
|
+
|
121
125
|
# Finds a row by searching based on primary key
|
122
126
|
# @param pk [Object] The primary key to look up using
|
123
127
|
# @return [Row,nil] The row that matches
|
@@ -182,11 +186,12 @@ class Chair
|
|
182
186
|
# @param column [Symbol] the column to be primary key
|
183
187
|
# @return [Symbol, nil] the primary key assigned. can be nil if the column doesn't exist
|
184
188
|
def set_primary_key!(column)
|
185
|
-
unless
|
186
|
-
|
189
|
+
unless has_primary_key?
|
190
|
+
check_column_exists column
|
191
|
+
remove_index!(column) if indices.include? column
|
192
|
+
@pk_map = build_pk_map column
|
193
|
+
@primary_key = column
|
187
194
|
end
|
188
|
-
@pk_map = build_pk_map column
|
189
|
-
@primary_key = column
|
190
195
|
end
|
191
196
|
|
192
197
|
# Does this table have a primary key?
|
@@ -242,8 +247,20 @@ class Chair
|
|
242
247
|
@table.last
|
243
248
|
end
|
244
249
|
|
245
|
-
|
250
|
+
def inspect
|
251
|
+
# Use the table's column list to order our columns
|
252
|
+
inspection = []
|
253
|
+
inspection << "primary_key: #{@primary_key.inspect}" if has_primary_key?
|
254
|
+
inspection << "indices: #{@indices.keys.inspect}" unless @indices.empty?
|
255
|
+
inspection << "columns: #{@columns.keys.inspect}" unless @columns.empty?
|
256
|
+
inspection = inspection.compact.join(', ')
|
257
|
+
unless inspection.empty?
|
258
|
+
inspection.insert 0, ' '
|
259
|
+
end
|
260
|
+
"#<#{self.class}#{inspection}>"
|
261
|
+
end
|
246
262
|
|
263
|
+
protected
|
247
264
|
def find_valid_indices(cols)
|
248
265
|
@indices.keys & cols
|
249
266
|
end
|
data/lib/chair/version.rb
CHANGED
data/spec/table_search_spec.rb
CHANGED
@@ -66,7 +66,7 @@ describe Chair do
|
|
66
66
|
expect(table.where_title_is('War and Peace').first.to_a).to eq([0, 'War and Peace', 'Leo Tolstoy'])
|
67
67
|
end
|
68
68
|
|
69
|
-
it 'table scan' do
|
69
|
+
it 'table scan by choice' do
|
70
70
|
table.insert! id: 0, title: 'War and Peace', author: 'Leo Tolstoy'
|
71
71
|
expect(table.table_scan(title: 'War and Peace').first.to_a).to eq([0, 'War and Peace', 'Leo Tolstoy'])
|
72
72
|
end
|
data/spec/table_spec.rb
CHANGED
@@ -80,6 +80,21 @@ describe Chair do
|
|
80
80
|
}.to change {table.primary_key}.from(nil).to(:title)
|
81
81
|
end
|
82
82
|
|
83
|
+
it "should raise an ArgumentError if it's not an existing column" do
|
84
|
+
table = Chair.new
|
85
|
+
expect{
|
86
|
+
table.set_primary_key! :title
|
87
|
+
}.to raise_error ArgumentError, "Column :title does not exist in table"
|
88
|
+
end
|
89
|
+
|
90
|
+
it "fails if there's already a primary key" do
|
91
|
+
table = Chair.new :title, :author
|
92
|
+
table.set_primary_key! :title
|
93
|
+
expect{
|
94
|
+
table.set_primary_key! :author
|
95
|
+
}.not_to change{table.primary_key}.from(:title)
|
96
|
+
end
|
97
|
+
|
83
98
|
describe 'fails to build index' do
|
84
99
|
it 'when duplicate fields exist' do
|
85
100
|
table = Chair.new :title
|
@@ -107,22 +122,15 @@ describe Chair do
|
|
107
122
|
table.set_primary_key! :title
|
108
123
|
}.to raise_error RuntimeError, 'Row does not have a value in column :title'
|
109
124
|
end
|
110
|
-
|
111
125
|
end
|
112
|
-
end
|
113
|
-
|
114
|
-
it "doesn't set the primary key if it's not a valid column" do
|
115
|
-
table = Chair.new
|
116
|
-
expect{
|
117
|
-
table.set_primary_key! :title
|
118
|
-
}.not_to change{table.primary_key}.from(nil)
|
119
|
-
end
|
120
126
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
127
|
+
it 'removes index on column' do
|
128
|
+
table = Chair.new :title
|
129
|
+
table.add_index! :title
|
130
|
+
expect {
|
131
|
+
table.set_primary_key! :title
|
132
|
+
}.to change {table.indices}.from([:title]).to([])
|
133
|
+
end
|
126
134
|
end
|
127
135
|
|
128
136
|
describe 'add index' do
|