christiank-turntable 0.3.2 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/turntable.rb +76 -16
  2. metadata +3 -3
data/turntable.rb CHANGED
@@ -1,14 +1,27 @@
1
1
  #
2
- # turntable v0.3.2
2
+ # turntable v0.4
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
+
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
8
17
 
9
18
  class Turntable
19
+
20
+ @@version = '0.4'
21
+
22
+ # Creates a new Turntable object and database file.
23
+ # Requires a filename and at least one column name.
10
24
  def initialize filename, *args
11
- # there must be at least one column
12
25
  raise "Turntable.new requires at least two arguments" if args.empty?
13
26
 
14
27
  @columns = []
@@ -22,7 +35,7 @@ class Turntable
22
35
  self.save_to @filename
23
36
  end
24
37
 
25
- # Turntable.load: loads the file and Marshals back the data
38
+ # Loads an external database file and returns the Turntable object.
26
39
  def Turntable.load filename
27
40
  raise "#{filename}: file does not exist" unless File.exists?(filename)
28
41
 
@@ -32,9 +45,17 @@ class Turntable
32
45
  f.close
33
46
  end
34
47
 
48
+ # Returns the version number.
49
+ def Turntable.version
50
+ @@version
51
+ end
52
+
53
+ # Reader methods for the object's columns and filename.
35
54
  attr_reader :columns
36
55
  attr_reader :filename
37
56
 
57
+ # Deletes a row as dictated by a hash {:id => some_value}.
58
+ # TODO: make sure it returns the deleted row(s).
38
59
  def delete hash
39
60
  raise 'delete() requires :id => some_value' unless hash.has_key?(:id)
40
61
 
@@ -42,16 +63,16 @@ class Turntable
42
63
  self.save_to @filename
43
64
  end
44
65
 
66
+ # Inserts a new row in the database. It requires that the number of arguments
67
+ # passed in equal the number of columns, just like any SQL INSERT.
68
+ # Columns named :id function like AUTOINCREMENT fields.
45
69
  def insert *args
46
- # the number of arguments must match the number of columns
47
70
  unless args.length == @columns.length
48
71
  raise "wrong number of arguments (#{args.length} for #{@columns.length})"
49
72
  end
50
73
 
51
74
  row = {}
52
75
 
53
- # columns named :id are like AUTOINCREMENT fields,
54
- # every other column is type-independent
55
76
  @columns.each do |column_name|
56
77
  if column_name == :id
57
78
  if @table.empty?
@@ -66,19 +87,18 @@ class Turntable
66
87
  end
67
88
  end
68
89
 
69
- # insert the data here:
70
90
  @table.push row
71
91
  self.save_to @filename
72
-
73
92
  return row
74
93
  end
75
94
 
76
- # inspect: this makes dealing with Turntable in IRB much easier
95
+ # Customize inspect() so dealing with Turntable in IRB is much cleaner.
77
96
  def inspect
78
97
  self.to_s
79
98
  end
80
99
 
81
- # puts_formatted: prints a "plain text" table
100
+ # Print a "plain text" table to STDOUT
101
+ # TODO: create a table.each.columns.each protected method, or something
82
102
  def puts_formatted
83
103
  @table.each do |row|
84
104
  @columns.each do |column|
@@ -90,7 +110,8 @@ class Turntable
90
110
  return nil
91
111
  end
92
112
 
93
- # select: equivalent to SELECT * WHERE statements
113
+ # Returns a subset of the table which satisfies given conditions.
114
+ # TODO: make sure it actually works
94
115
  def select hash
95
116
  result_set = []
96
117
 
@@ -105,12 +126,12 @@ class Turntable
105
126
  return result_set
106
127
  end
107
128
 
129
+ # Returns the entire table, an array of hashes.
108
130
  def select_all
109
131
  return @table
110
132
  end
111
133
 
112
- # update: replace many columns at a time,
113
- # but only on one row at a time
134
+ # Replaces many columns at a time, but currently only on one row at a time.
114
135
  def update hash
115
136
  raise 'update() requires at least :id => some_value' unless hash.has_key?(:id)
116
137
 
@@ -125,8 +146,47 @@ class Turntable
125
146
  return @table[id]
126
147
  end
127
148
 
149
+ # Rearranges the entire database by a specified column
150
+ def order_by column_name
151
+ unless @columns.include?(column_name)
152
+ raise "column \"#{column_name}\" is not declared in this Turntable"
153
+ end
154
+
155
+ # convert all row-hashes into arrays
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)
162
+ end
163
+ end
164
+
165
+ # switch the first element of each row with the desired order_by column
166
+ # (the first element is where sort() looks)
167
+ rearranged_table.each do |row|
168
+ row.switch 0, ( row.index(row.assoc(column_name.to_s)) )
169
+ end
170
+
171
+ # properly arrange the table
172
+ rearranged_table.sort!
173
+
174
+ # convert everything back to hashes and symbols
175
+ rearranged_table.collect do |row|
176
+ this_row_hash = {}
177
+ this_column_hash = {}
178
+
179
+ row.each do |column|
180
+ column[0] = column[0].to_sym
181
+ this_column_hash[ column[0] ] = column[1]
182
+ end
183
+
184
+ this_row_hash.update this_column_hash
185
+ end
186
+ end
187
+
128
188
  protected
129
- # save_to: marshals in the Turntable object
189
+ # Marshals the database object to an external file.
130
190
  def save_to filename
131
191
  if File.exists? filename
132
192
  f = File.open filename, 'w'
@@ -137,4 +197,4 @@ class Turntable
137
197
  Marshal.dump self, f
138
198
  f.close
139
199
  end
140
- end
200
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: christiank-turntable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: "0.4"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Koch
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-21 00:00:00 -07:00
12
+ date: 2009-05-22 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -48,6 +48,6 @@ rubyforge_project:
48
48
  rubygems_version: 1.2.0
49
49
  signing_key:
50
50
  specification_version: 2
51
- summary: Turntable is an alternative to relational databases for Ruby applications.
51
+ summary: Turntable is an alternative to relational databases for Ruby.
52
52
  test_files: []
53
53