christiank-turntable 0.4.2 → 0.4.3

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 +30 -13
  2. metadata +2 -2
data/turntable.rb CHANGED
@@ -1,19 +1,19 @@
1
1
  #
2
- # turntable v0.4.2
2
+ # turntable v0.4.3
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
9
  class Turntable
10
-
11
- @@version = '0.4.2'
10
+
11
+ @@version = '0.4.3'
12
12
 
13
13
  # Creates a new Turntable object and database file.
14
14
  # Requires a filename and at least one column name.
15
15
  def initialize filename, *args
16
- raise "Turntable.new requires at least two arguments" if args.empty?
16
+ raise 'Turntable.new requires at least two arguments' if args.empty?
17
17
 
18
18
  @columns = []
19
19
  @table = []
@@ -44,6 +44,13 @@ 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
+ # Pushes a new column. Fills the new fields with the empty string
49
+ # (because filling them with nil creates issues). Returns the the new table.
50
+ def add_column column_name
51
+ @columns.push column_name
52
+ @table.each { |row| row[column_name] = '' }
53
+ end
47
54
 
48
55
  # Returns true or false, depending on whether the result is empty.
49
56
  def contains? hash
@@ -55,7 +62,7 @@ class Turntable
55
62
  raise 'delete() requires :id => some_value' unless hash.has_key?(:id)
56
63
 
57
64
  before_table = @table.dup
58
-
65
+
59
66
  # Delete the table for real here:
60
67
  what_wasnt_deleted = @table.delete_if { |row| row[:id] == hash[:id] }
61
68
  self.save_to @filename
@@ -64,6 +71,12 @@ class Turntable
64
71
  return before_table - what_wasnt_deleted
65
72
  end
66
73
 
74
+ # Eliminates an entire column.
75
+ def drop_column column_name
76
+ @columns.delete_if { |column| column == column_name}
77
+ @table.each { |row| row.delete column_name }
78
+ end
79
+
67
80
  # Inserts a new row in the database. It requires that the number of arguments
68
81
  # passed in equal the number of columns, just like any SQL INSERT.
69
82
  # Columns named :id function like AUTOINCREMENT fields.
@@ -101,13 +114,17 @@ class Turntable
101
114
  # Print a "plain text" table to STDOUT
102
115
  # TODO: create a table.each.columns.each protected method, or something
103
116
  def puts_formatted
117
+ warn 'Warning: puts_formatted() is deprecated, use to_stdout()'
118
+ to_stdout
119
+ end
120
+
121
+ def to_stdout
104
122
  @table.each do |row|
105
123
  @columns.each do |column|
106
124
  print "|#{row[column]}"
107
125
  end
108
126
  puts "|"
109
127
  end
110
-
111
128
  return nil
112
129
  end
113
130
 
@@ -129,7 +146,7 @@ class Turntable
129
146
 
130
147
  # Returns the entire table, an array of hashes.
131
148
  def select_all
132
- return @table
149
+ @table
133
150
  end
134
151
 
135
152
  alias :all :select_all
@@ -150,7 +167,7 @@ class Turntable
150
167
  end
151
168
 
152
169
  protected
153
- # Marshals the database object to an external file.
170
+ # Marshals the database object to an external file.
154
171
  def save_to filename
155
172
  if File.exists? filename
156
173
  f = File.open filename, 'w'
@@ -162,9 +179,9 @@ class Turntable
162
179
  f.close
163
180
  end
164
181
  end
165
-
182
+
166
183
  class Array
167
-
184
+
168
185
  # Rearranges an array of hashes according to a specified column name.
169
186
  # TODO: how to check for @columns, even though it's a Turntable variable?
170
187
  def order_by column_name
@@ -172,7 +189,7 @@ class Array
172
189
  # Convert all row-hashes into arrays
173
190
  rearranged_table = self.collect { |row| row.to_a }
174
191
 
175
- # Convert all column-name-symbols into strings,
192
+ # Convert all column-name-symbols into strings,
176
193
  # then switch the first element of each row the desired order_by column
177
194
  # (because the first element is where sort() looks)
178
195
  rearranged_table.each do |row|
@@ -189,7 +206,7 @@ class Array
189
206
  column[0] = column[0].to_sym
190
207
  this_column_hash[ column[0] ] = column[1]
191
208
  end
192
-
209
+
193
210
  this_row_hash.update this_column_hash
194
211
  end
195
212
  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.4.2
4
+ version: 0.4.3
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-22 00:00:00 -07:00
12
+ date: 2009-05-24 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15