christiank-turntable 0.4 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/turntable.rb +37 -45
- metadata +1 -1
data/turntable.rb
CHANGED
@@ -1,23 +1,14 @@
|
|
1
1
|
#
|
2
|
-
# turntable v0.4
|
2
|
+
# turntable v0.4.1
|
3
3
|
# Christian Koch < ckoch002@student.ucr.edu >
|
4
4
|
#
|
5
5
|
# This is free software. You are permitted to modify, implement, and otherwise
|
6
6
|
# use this source code freely (as in beer and freedom).
|
7
7
|
#
|
8
8
|
|
9
|
-
class Array
|
10
|
-
# Exchanges two elements' positions within an array.
|
11
|
-
def switch index1, index2
|
12
|
-
old_array = self.dup
|
13
|
-
self[index1] = old_array[index2]
|
14
|
-
self[index2] = old_array[index1]
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
9
|
class Turntable
|
19
10
|
|
20
|
-
@@version = '0.4'
|
11
|
+
@@version = '0.4.1'
|
21
12
|
|
22
13
|
# Creates a new Turntable object and database file.
|
23
14
|
# Requires a filename and at least one column name.
|
@@ -111,7 +102,7 @@ class Turntable
|
|
111
102
|
end
|
112
103
|
|
113
104
|
# Returns a subset of the table which satisfies given conditions.
|
114
|
-
# TODO:
|
105
|
+
# TODO: get compound conditions to work somehow
|
115
106
|
def select hash
|
116
107
|
result_set = []
|
117
108
|
|
@@ -146,33 +137,39 @@ class Turntable
|
|
146
137
|
return @table[id]
|
147
138
|
end
|
148
139
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
rearranged_table = @table.collect { |row| row.to_a }
|
157
|
-
|
158
|
-
# convert all column-name-symbols into strings
|
159
|
-
rearranged_table.each do |row|
|
160
|
-
row.each do |column|
|
161
|
-
column[0] = column[0].to_s if (column[0].class == Symbol)
|
140
|
+
protected
|
141
|
+
# Marshals the database object to an external file.
|
142
|
+
def save_to filename
|
143
|
+
if File.exists? filename
|
144
|
+
f = File.open filename, 'w'
|
145
|
+
else
|
146
|
+
f = File.new filename, 'w'
|
162
147
|
end
|
148
|
+
|
149
|
+
Marshal.dump self, f
|
150
|
+
f.close
|
163
151
|
end
|
152
|
+
end
|
153
|
+
|
154
|
+
class Array
|
155
|
+
|
156
|
+
# Rearranges an array of hashes according to a specified column name.
|
157
|
+
# TODO: how to check for @columns, even though it's a Turntable variable?
|
158
|
+
def order_by column_name
|
159
|
+
|
160
|
+
# Convert all row-hashes into arrays
|
161
|
+
rearranged_table = self.collect { |row| row.to_a }
|
164
162
|
|
165
|
-
#
|
166
|
-
#
|
163
|
+
# Convert all column-name-symbols into strings,
|
164
|
+
# then switch the first element of each row the desired order_by column
|
165
|
+
# (because the first element is where sort() looks)
|
167
166
|
rearranged_table.each do |row|
|
167
|
+
row.each { |column| column[0] = column[0].to_s if (column[0].class == Symbol) }
|
168
168
|
row.switch 0, ( row.index(row.assoc(column_name.to_s)) )
|
169
169
|
end
|
170
170
|
|
171
|
-
# properly
|
172
|
-
rearranged_table.sort
|
173
|
-
|
174
|
-
# convert everything back to hashes and symbols
|
175
|
-
rearranged_table.collect do |row|
|
171
|
+
# Sort everything properly, and convert it back to hashes and symbols
|
172
|
+
rearranged_table.sort.collect do |row|
|
176
173
|
this_row_hash = {}
|
177
174
|
this_column_hash = {}
|
178
175
|
|
@@ -185,16 +182,11 @@ class Turntable
|
|
185
182
|
end
|
186
183
|
end
|
187
184
|
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
Marshal.dump self, f
|
198
|
-
f.close
|
199
|
-
end
|
200
|
-
end
|
185
|
+
# Exchanges two elements' positions within an array.
|
186
|
+
def switch index1, index2
|
187
|
+
old_array = self.dup
|
188
|
+
self[index1] = old_array[index2]
|
189
|
+
self[index2] = old_array[index1]
|
190
|
+
end
|
191
|
+
|
192
|
+
end
|