christiank-turntable 0.7 → 0.7.1

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 +48 -98
  2. metadata +2 -2
data/turntable.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #
2
- # turntable v0.7
2
+ # turntable v0.7.1
3
3
  # Christian Koch <ckoch002@student.ucr.edu>
4
4
  #
5
5
 
@@ -8,44 +8,25 @@ require 'ostruct'
8
8
 
9
9
  class Turntable < Array
10
10
 
11
- @@version = 'v0.7'
12
-
13
- @@insert_proc = %q(
14
- if args.length != @columns.length
15
- raise ArgumentError, "wrong number of arguments (#{args.length} for #{@columns.length})"
16
- end
17
-
18
- if @types
19
- test_types = @types.dup
20
- args.each do |arg|
21
- type_should_be = test_types.shift
22
- raise TypeError, "expected #{type_should_be} instead of #{arg.class}" unless arg.is_a?(type_should_be)
23
- end
24
- end
25
-
26
- row = Row.new
27
- self.empty? ? (row.id = 0) : (row.id = self.last.id + 1)
28
- @columns.each { |column| eval "row.#{column} = args.shift" }
29
- self.push row
30
- self.save_to @filename
31
- row
32
- )
11
+ @@version = 'v0.7.1'
33
12
 
34
13
  # Creates a new Turntable database. Accepts either a list of column names, or
35
- # an array of symbols which contain the same thing.
14
+ # an array of symbols which contain the same thing. Requires at least 2
15
+ # column names.
36
16
  def initialize filename, *columns
37
- if columns.empty?
38
- raise ArgumentError, 'Turntable.new() requires a filename and at least one column name'
39
- end
40
-
41
- if columns.length == 1 and columns[0].is_a?(Array) and columns[0][0].is_a?(Symbol)
42
- @columns = columns[0]
17
+ if columns.length == 1
18
+ if columns[0].is_a?(Array) and (columns.all? { |c| c.is_a?(Symbol) })
19
+ @columns = columns[0]
20
+ else
21
+ raise ArgumentError, 'Turntable.new() requires a filename and at least two column names'
22
+ end
23
+ elsif columns.empty?
24
+ raise ArgumentError, 'Turntable.new() requires a filename and at least two column names'
43
25
  else
44
26
  @columns = columns
45
27
  end
46
28
 
47
29
  @filename = File.expand_path(filename)
48
- @types = nil
49
30
 
50
31
  self.save_to @filename
51
32
  end
@@ -69,34 +50,13 @@ class Turntable < Array
69
50
 
70
51
  # Reader methods for this Turntable's metadata.
71
52
  def Turntable.version; @@version; end
72
- attr_reader :filename, :columns, :types
73
-
74
- # Inserts a new column to the database. If the table is strictly typed, then
75
- # all the preexisting rows are filled with new objects of that class. If the
76
- # class is numeric, then the rows are filled with the appropriate form of 0.
77
- # Otherwise, they're filled with just nil. Returns the new columns list.
78
- def add_column column_name, column_type=nil
79
- if @types
80
- raise ArgumentError, 'expecting a class name' unless column_type.is_a?(Class)
81
- @columns.push column_name
82
- @types.push column_type
83
-
84
- # For some reason you can't switch through classes, but you can if
85
- # it's a string
86
- case column_type.inspect
87
- when 'Fixnum', 'Integer'
88
- what_to_add = "0"
89
- when 'Float'
90
- what_to_add = "0.0"
91
- else
92
- what_to_add = "#{column_type}.new"
93
- end
53
+ attr_reader :filename, :columns
94
54
 
95
- self.each { |row| eval "row.#{column_name} = #{what_to_add}" }
96
- else
97
- @columns.push column_name
98
- self.each { |row| eval "row.#{column_name} = nil" }
99
- end
55
+ # Inserts a new column to the database. All preexisting rows are filled with
56
+ # nil. Returns the new columns list.
57
+ def add_column column_name
58
+ @columns.push column_name
59
+ self.each { |row| eval "row.#{column_name} = nil" }
100
60
 
101
61
  self.save_to @filename
102
62
  self.columns
@@ -118,24 +78,23 @@ class Turntable < Array
118
78
  result.any?
119
79
  end
120
80
 
121
- # Pushes self with a new row. Returns the newly created row. Requires the
122
- # number of arguments passed in equal the number of columns in the database,
123
- # or it can accept calls from Turntable#insert_array.
81
+ # Pushes self with a new row. It accepts a list of values to be inserted, or
82
+ # one array which contains the same thing. Returns the newly created row.
124
83
  def insert *args
125
- eval @@insert_proc
126
- end
84
+ args = one_array_or_list(args, @columns)
127
85
 
128
- # Same as Turntable#insert, except this method accepts only one array filled
129
- # with values.
130
- # TODO: this isn't necessary
131
- def insert_array args
132
- raise ArgumentError, 'Turntable#insert_array requires one array' unless args.is_a?(Array)
133
- eval @@insert_proc
134
- end
86
+ row = Row.new
87
+ self.empty? ? (row.id = 0) : (row.id = self.last.id + 1)
88
+ @columns.each { |column| eval "row.#{column} = args.shift" }
89
+ self.push row
135
90
 
91
+ self.save_to @filename
92
+ row
93
+ end
94
+
136
95
  # Custom inspect() makes working in IRB much cleaner.
137
96
  def inspect
138
- "#<#{self.class}:#{self.object_id} @filename=#{self.filename.inspect}, @columns=#{self.columns.inspect}, @types=#{self.types.inspect}>"
97
+ "#<#{self.class}:#{self.object_id} @filename=#{self.filename.inspect}, @columns=#{self.columns.inspect}>"
139
98
  end
140
99
 
141
100
  # Prints the entire database to STDOUT. It's just a wrapper for
@@ -151,33 +110,27 @@ class Turntable < Array
151
110
  self.save_to @filename
152
111
  what_happened
153
112
  end
154
-
155
- # Causes self to become strictly typed. Accepts either a list of classes, or
156
- # one array which contains the same thing.
157
- def use_strict_typing! *args
158
- if args.length == 1
159
- unless args[0].is_a?(Array) and (args[0].length == @columns.length)
160
- raise ArgumentError, "wrong number of elements (#{args.length} for #{@columns.length})"
161
- end
162
- args = args[0]
163
- else
164
- if args.length != @columns.length
165
- raise ArgumentError, "wrong number of arguments (#{args.length} for #{@columns.length})"
166
- end
167
- end
168
113
 
169
- # All arguments must be classes.
170
- if args.select { |arg| arg.is_a?(Class) } == args
171
- @types = args
172
- else
173
- raise TypeError, "use_strict_typing!() requires a list of classes"
174
- end
114
+ protected
115
+ # Users shouldn't reassign @filename themselves.
116
+ attr_writer :filename
175
117
 
176
- self.save_to @filename
177
- @types
178
- end
118
+ # Checks whether the user provided one array of values, or a list which
119
+ # contains the same thing.
120
+ def one_array_or_list args, columns
121
+ if args.length == 1
122
+ unless args[0].is_a?(Array) and (args[0].length == columns.length)
123
+ raise ArgumentError, "wrong number of elements (#{args.length} for #{columns.length})"
124
+ end
125
+ return args[0]
126
+ else
127
+ if args.length != @columns.length
128
+ raise ArgumentError, "wrong number of arguments (#{args.length} for #{columns.length})"
129
+ end
130
+ return args
131
+ end
132
+ end
179
133
 
180
- protected
181
134
  # Saves self to an external file.
182
135
  def save_to filename
183
136
  f = File.new filename, 'w'
@@ -185,9 +138,6 @@ class Turntable < Array
185
138
  f.close
186
139
  end
187
140
 
188
- # Users shouldn't reassign @filename themselves.
189
- attr_writer :filename
190
-
191
141
  end
192
142
 
193
143
  class Array
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.7"
4
+ version: 0.7.1
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-07-01 00:00:00 -07:00
12
+ date: 2009-07-07 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15