christiank-turntable 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/turntable.rb +19 -7
- metadata +1 -1
data/turntable.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#
|
2
|
-
# turntable v0.4.
|
2
|
+
# turntable v0.4.2
|
3
3
|
# Christian Koch < ckoch002@student.ucr.edu >
|
4
4
|
#
|
5
5
|
# This is free software. You are permitted to modify, implement, and otherwise
|
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
class Turntable
|
10
10
|
|
11
|
-
@@version = '0.4.
|
11
|
+
@@version = '0.4.2'
|
12
12
|
|
13
13
|
# Creates a new Turntable object and database file.
|
14
14
|
# Requires a filename and at least one column name.
|
@@ -44,14 +44,24 @@ class Turntable
|
|
44
44
|
# Reader methods for the object's columns and filename.
|
45
45
|
attr_reader :columns
|
46
46
|
attr_reader :filename
|
47
|
+
|
48
|
+
# Returns true or false, depending on whether the result is empty.
|
49
|
+
def contains? hash
|
50
|
+
!self.select(hash).empty?
|
51
|
+
end
|
47
52
|
|
48
53
|
# Deletes a row as dictated by a hash {:id => some_value}.
|
49
|
-
# TODO: make sure it returns the deleted row(s).
|
50
54
|
def delete hash
|
51
55
|
raise 'delete() requires :id => some_value' unless hash.has_key?(:id)
|
52
56
|
|
53
|
-
@table.
|
57
|
+
before_table = @table.dup
|
58
|
+
|
59
|
+
# Delete the table for real here:
|
60
|
+
what_wasnt_deleted = @table.delete_if { |row| row[:id] == hash[:id] }
|
54
61
|
self.save_to @filename
|
62
|
+
|
63
|
+
# Return the deleted row here:
|
64
|
+
return before_table - what_wasnt_deleted
|
55
65
|
end
|
56
66
|
|
57
67
|
# Inserts a new row in the database. It requires that the number of arguments
|
@@ -116,12 +126,14 @@ class Turntable
|
|
116
126
|
|
117
127
|
return result_set
|
118
128
|
end
|
119
|
-
|
129
|
+
|
120
130
|
# Returns the entire table, an array of hashes.
|
121
131
|
def select_all
|
122
132
|
return @table
|
123
133
|
end
|
124
134
|
|
135
|
+
alias :all :select_all
|
136
|
+
|
125
137
|
# Replaces many columns at a time, but currently only on one row at a time.
|
126
138
|
def update hash
|
127
139
|
raise 'update() requires at least :id => some_value' unless hash.has_key?(:id)
|
@@ -172,12 +184,12 @@ class Array
|
|
172
184
|
rearranged_table.sort.collect do |row|
|
173
185
|
this_row_hash = {}
|
174
186
|
this_column_hash = {}
|
175
|
-
|
187
|
+
|
176
188
|
row.each do |column|
|
177
189
|
column[0] = column[0].to_sym
|
178
190
|
this_column_hash[ column[0] ] = column[1]
|
179
191
|
end
|
180
|
-
|
192
|
+
|
181
193
|
this_row_hash.update this_column_hash
|
182
194
|
end
|
183
195
|
end
|