christiank-turntable 0.999.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dictionary.rb +37 -134
- data/turntable.rb +16 -6
- metadata +1 -1
data/lib/dictionary.rb
CHANGED
@@ -42,45 +42,14 @@ class Dictionary
|
|
42
42
|
end
|
43
43
|
hsh
|
44
44
|
end
|
45
|
-
|
46
|
-
# Like #new but the block sets the order.
|
47
|
-
def new_by(*args, &blk)
|
48
|
-
new(*args).order_by(&blk)
|
49
|
-
end
|
50
|
-
|
51
|
-
# Alternate to #new which creates a dictionary sorted by key.
|
52
|
-
#
|
53
|
-
# d = Dictionary.alpha
|
54
|
-
# d["z"] = 1
|
55
|
-
# d["y"] = 2
|
56
|
-
# d["x"] = 3
|
57
|
-
# d #=> {"x"=>3,"y"=>2,"z"=>2}
|
58
|
-
#
|
59
|
-
# This is equivalent to:
|
60
|
-
#
|
61
|
-
# Dictionary.new.order_by { |key,value| key }
|
62
|
-
def alpha(*args, &block)
|
63
|
-
new(*args, &block).order_by_key
|
64
|
-
end
|
65
|
-
|
66
|
-
# Alternate to #new which auto-creates sub-dictionaries as needed.
|
67
|
-
#
|
68
|
-
# d = Dictionary.auto
|
69
|
-
# d["a"]["b"]["c"] = "abc" #=> { "a"=>{"b"=>{"c"=>"abc"}}}
|
70
|
-
#
|
71
|
-
def auto(*args)
|
72
|
-
leet = lambda { |hsh, key| hsh[key] = new(&leet) }
|
73
|
-
new(*args, &leet)
|
74
|
-
end
|
75
45
|
end
|
76
46
|
|
77
|
-
# New Dictionary.
|
78
47
|
def initialize(*args, &blk)
|
79
48
|
@order = []
|
80
49
|
@order_by = nil
|
81
50
|
if blk
|
82
|
-
dict = self
|
83
|
-
oblk = lambda{ |hsh, key| blk[dict,key] }
|
51
|
+
dict = self
|
52
|
+
oblk = lambda{ |hsh, key| blk[dict,key] }
|
84
53
|
@hash = Hash.new(*args, &oblk)
|
85
54
|
else
|
86
55
|
@hash = Hash.new(*args)
|
@@ -92,43 +61,18 @@ class Dictionary
|
|
92
61
|
@order
|
93
62
|
end
|
94
63
|
|
95
|
-
|
96
|
-
def order_by( &block )
|
64
|
+
def order_by(&block)
|
97
65
|
@order_by = block
|
98
66
|
order
|
99
67
|
self
|
100
68
|
end
|
101
69
|
|
102
|
-
# Keep dictionary sorted by key.
|
103
|
-
#
|
104
|
-
# d = Dictionary.new.order_by_key
|
105
|
-
# d["z"] = 1
|
106
|
-
# d["y"] = 2
|
107
|
-
# d["x"] = 3
|
108
|
-
# d #=> {"x"=>3,"y"=>2,"z"=>2}
|
109
|
-
#
|
110
|
-
# This is equivalent to:
|
111
|
-
#
|
112
|
-
# Dictionary.new.order_by { |key,value| key }
|
113
|
-
#
|
114
|
-
# The initializer Dictionary#alpha also provides this.
|
115
70
|
def order_by_key
|
116
71
|
@order_by = lambda { |k,v| k }
|
117
72
|
order
|
118
73
|
self
|
119
74
|
end
|
120
75
|
|
121
|
-
# Keep dictionary sorted by value.
|
122
|
-
#
|
123
|
-
# d = Dictionary.new.order_by_value
|
124
|
-
# d["z"] = 1
|
125
|
-
# d["y"] = 2
|
126
|
-
# d["x"] = 3
|
127
|
-
# d #=> {"x"=>3,"y"=>2,"z"=>2}
|
128
|
-
#
|
129
|
-
# This is equivalent to:
|
130
|
-
#
|
131
|
-
# Dictionary.new.order_by { |key,value| value }
|
132
76
|
def order_by_value
|
133
77
|
@order_by = lambda { |k,v| v }
|
134
78
|
order
|
@@ -144,7 +88,7 @@ class Dictionary
|
|
144
88
|
end
|
145
89
|
|
146
90
|
def ==(hsh2)
|
147
|
-
if hsh2.is_a?(
|
91
|
+
if hsh2.is_a?(Dictionary)
|
148
92
|
@order == hsh2.order &&
|
149
93
|
@hash == hsh2.instance_variable_get("@hash")
|
150
94
|
else
|
@@ -152,21 +96,9 @@ class Dictionary
|
|
152
96
|
end
|
153
97
|
end
|
154
98
|
|
155
|
-
def [] k
|
156
|
-
|
157
|
-
end
|
99
|
+
def [](k); @hash[k]; end
|
100
|
+
def fetch(k, *a, &b); @hash.fetch(k, *a, &b); end
|
158
101
|
|
159
|
-
def fetch(k, *a, &b)
|
160
|
-
@hash.fetch(k, *a, &b)
|
161
|
-
end
|
162
|
-
|
163
|
-
# Store operator.
|
164
|
-
#
|
165
|
-
# h[key] = value
|
166
|
-
#
|
167
|
-
# Or with additional index.
|
168
|
-
#
|
169
|
-
# h[key,index] = value
|
170
102
|
def []=(k, i=nil, v=nil)
|
171
103
|
if v
|
172
104
|
insert(i,k,v)
|
@@ -175,14 +107,14 @@ class Dictionary
|
|
175
107
|
end
|
176
108
|
end
|
177
109
|
|
178
|
-
def insert(
|
179
|
-
@order.insert(
|
180
|
-
@hash.store(
|
110
|
+
def insert(i, k, v)
|
111
|
+
@order.insert(i, k)
|
112
|
+
@hash.store(k, v)
|
181
113
|
end
|
182
114
|
|
183
|
-
def store(
|
184
|
-
@order.push(
|
185
|
-
@hash.store(
|
115
|
+
def store(a, b)
|
116
|
+
@order.push(a) unless @hash.has_key?(a)
|
117
|
+
@hash.store(a,b)
|
186
118
|
end
|
187
119
|
|
188
120
|
def clear
|
@@ -190,23 +122,23 @@ class Dictionary
|
|
190
122
|
@hash.clear
|
191
123
|
end
|
192
124
|
|
193
|
-
def delete(
|
194
|
-
@order.delete(
|
195
|
-
@hash.delete(
|
125
|
+
def delete(key)
|
126
|
+
@order.delete(key)
|
127
|
+
@hash.delete(key)
|
196
128
|
end
|
197
129
|
|
198
130
|
def each_key
|
199
|
-
order.each { |k| yield(
|
131
|
+
order.each { |k| yield(k) }
|
200
132
|
self
|
201
133
|
end
|
202
134
|
|
203
135
|
def each_value
|
204
|
-
order.each { |k| yield(
|
136
|
+
order.each { |k| yield(@hash[k]) }
|
205
137
|
self
|
206
138
|
end
|
207
139
|
|
208
140
|
def each
|
209
|
-
order.each { |k| yield(
|
141
|
+
order.each { |k| yield(k,@hash[k]) }
|
210
142
|
self
|
211
143
|
end
|
212
144
|
alias each_pair each
|
@@ -222,9 +154,7 @@ class Dictionary
|
|
222
154
|
ary
|
223
155
|
end
|
224
156
|
|
225
|
-
def keys
|
226
|
-
order
|
227
|
-
end
|
157
|
+
def keys; order; end
|
228
158
|
|
229
159
|
def invert
|
230
160
|
hsh2 = self.class.new
|
@@ -232,11 +162,9 @@ class Dictionary
|
|
232
162
|
hsh2
|
233
163
|
end
|
234
164
|
|
235
|
-
def reject(&block)
|
236
|
-
self.dup.delete_if(&block)
|
237
|
-
end
|
165
|
+
def reject(&block); self.dup.delete_if(&block); end
|
238
166
|
|
239
|
-
def reject!(
|
167
|
+
def reject!(&block)
|
240
168
|
hsh2 = reject(&block)
|
241
169
|
self == hsh2 ? nil : hsh2
|
242
170
|
end
|
@@ -258,7 +186,7 @@ class Dictionary
|
|
258
186
|
key ? [key,delete(key)] : super
|
259
187
|
end
|
260
188
|
|
261
|
-
def unshift(
|
189
|
+
def unshift(k,v)
|
262
190
|
unless @hash.include?( k )
|
263
191
|
@order.unshift( k )
|
264
192
|
@hash.store( k,v )
|
@@ -268,14 +196,12 @@ class Dictionary
|
|
268
196
|
end
|
269
197
|
end
|
270
198
|
|
271
|
-
def <<(kv)
|
272
|
-
push(*kv)
|
273
|
-
end
|
199
|
+
def <<(kv); push(*kv); end
|
274
200
|
|
275
|
-
def push(
|
276
|
-
unless @hash.include?(
|
277
|
-
@order.push(
|
278
|
-
@hash.store(
|
201
|
+
def push(k, v)
|
202
|
+
unless @hash.include?(k)
|
203
|
+
@order.push(k)
|
204
|
+
@hash.store(k, v)
|
279
205
|
true
|
280
206
|
else
|
281
207
|
false
|
@@ -299,16 +225,14 @@ class Dictionary
|
|
299
225
|
self.class[*a]
|
300
226
|
end
|
301
227
|
|
302
|
-
def update(
|
228
|
+
def update(hsh2)
|
303
229
|
hsh2.each { |k,v| self[k] = v }
|
304
230
|
reorder
|
305
231
|
self
|
306
232
|
end
|
307
233
|
alias :merge! update
|
308
234
|
|
309
|
-
def merge( hsh2
|
310
|
-
self.dup.update(hsh2)
|
311
|
-
end
|
235
|
+
def merge(hsh2); self.dup.update(hsh2); end
|
312
236
|
|
313
237
|
def select
|
314
238
|
ary = []
|
@@ -321,9 +245,7 @@ class Dictionary
|
|
321
245
|
self
|
322
246
|
end
|
323
247
|
|
324
|
-
def reverse
|
325
|
-
dup.reverse!
|
326
|
-
end
|
248
|
+
def reverse; dup.reverse!; end
|
327
249
|
|
328
250
|
def first(x=nil)
|
329
251
|
return @hash[order.first] unless x
|
@@ -335,22 +257,11 @@ class Dictionary
|
|
335
257
|
order.last(x).collect { |k| @hash[k] }
|
336
258
|
end
|
337
259
|
|
338
|
-
def length
|
339
|
-
@order.length
|
340
|
-
end
|
260
|
+
def length; @order.length; end
|
341
261
|
alias :size :length
|
342
|
-
|
343
|
-
def
|
344
|
-
|
345
|
-
end
|
346
|
-
|
347
|
-
def has_key?(key)
|
348
|
-
@hash.has_key?(key)
|
349
|
-
end
|
350
|
-
|
351
|
-
def key?(key)
|
352
|
-
@hash.key?(key)
|
353
|
-
end
|
262
|
+
def empty?; @hash.empty?; end
|
263
|
+
def has_key?(key); @hash.has_key?(key); end
|
264
|
+
def key?(key); @hash.key?(key); end
|
354
265
|
|
355
266
|
def to_a
|
356
267
|
ary = []
|
@@ -358,15 +269,7 @@ class Dictionary
|
|
358
269
|
ary
|
359
270
|
end
|
360
271
|
|
361
|
-
def to_s
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
def to_hash
|
366
|
-
@hash.dup
|
367
|
-
end
|
368
|
-
|
369
|
-
def to_h
|
370
|
-
@hash.dup
|
371
|
-
end
|
272
|
+
def to_s; self.to_a.to_s; end
|
273
|
+
def to_hash; @hash.dup; end
|
274
|
+
alias :to_h :to_hash
|
372
275
|
end
|
data/turntable.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#
|
2
|
-
# Turntable
|
2
|
+
# Turntable v1.0.0
|
3
3
|
# Christian Koch <ckoch002@student.ucr.edu>
|
4
4
|
#
|
5
5
|
# Turntable is an alternative relational database for Ruby. A Turntable object
|
@@ -13,7 +13,7 @@ require 'ostruct'
|
|
13
13
|
|
14
14
|
class Turntable
|
15
15
|
|
16
|
-
VERSION = '
|
16
|
+
VERSION = 'v1.0.0'
|
17
17
|
|
18
18
|
include Enumerable
|
19
19
|
def each
|
@@ -78,6 +78,8 @@ class Turntable
|
|
78
78
|
@table.each_key { |key| result.push(@table[key]) if key.is_a?(Fixnum) }
|
79
79
|
result
|
80
80
|
end
|
81
|
+
alias :all :all_numbered_rows
|
82
|
+
alias :* :all_numbered_rows
|
81
83
|
|
82
84
|
# Removes a given row. Returns the new database.
|
83
85
|
def delete id
|
@@ -85,17 +87,25 @@ class Turntable
|
|
85
87
|
self.update
|
86
88
|
@table
|
87
89
|
end
|
88
|
-
|
90
|
+
|
89
91
|
# A much healthier inspect() makes dealing with Turntable in IRB easier.
|
90
92
|
def inspect; self.to_s; end
|
91
93
|
|
94
|
+
# Returns a boolean indicating whether the select statement would return any
|
95
|
+
# data.
|
96
|
+
def include?
|
97
|
+
result = []
|
98
|
+
self.each { |row| result.push row if yield row }
|
99
|
+
result.any?
|
100
|
+
end
|
101
|
+
|
92
102
|
# Returns a dictionary of all this Turntable's metadata.
|
93
103
|
def metadata
|
94
104
|
result = Dictionary.new
|
95
105
|
@table.each { |key, value| result[key] = value unless key.is_a?(Fixnum) }
|
96
106
|
result
|
97
107
|
end
|
98
|
-
|
108
|
+
|
99
109
|
# Pushes self with a new row. Accepts either a list of arguments, or one
|
100
110
|
# array which represents the same thing.
|
101
111
|
def push *args
|
@@ -104,7 +114,7 @@ class Turntable
|
|
104
114
|
if args.length != (@table[:columns].length - 1)
|
105
115
|
raise ArgumentError, "wrong number of arguments (#{args.length} for #{@table[:columns].length - 1})"
|
106
116
|
end
|
107
|
-
|
117
|
+
|
108
118
|
@before_table = @table.dup
|
109
119
|
|
110
120
|
row = Row.new
|
@@ -181,7 +191,7 @@ class Object
|
|
181
191
|
|
182
192
|
# Turn off Object#id warnings.
|
183
193
|
undef id
|
184
|
-
|
194
|
+
|
185
195
|
# Just like regular puts(), except putting a Turntable calls
|
186
196
|
# Turntable#to_textile.
|
187
197
|
def puts obj
|