christiank-turntable 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/turntable.rb +26 -13
- metadata +3 -3
data/turntable.rb
CHANGED
@@ -13,7 +13,7 @@ require 'ostruct'
|
|
13
13
|
|
14
14
|
class Turntable
|
15
15
|
|
16
|
-
VERSION = 'v1.1.
|
16
|
+
VERSION = 'v1.1.2'
|
17
17
|
|
18
18
|
include Enumerable
|
19
19
|
def each
|
@@ -22,6 +22,10 @@ class Turntable
|
|
22
22
|
|
23
23
|
# Creates a new Turntable object and corresponding file. Requires a filename
|
24
24
|
# and a list of column names, or one array which represents the column names.
|
25
|
+
#
|
26
|
+
# Column names should be symbols. There is no need to declare an :id or
|
27
|
+
# primary key column; this is automatically taken care of by virtue of
|
28
|
+
# Turntable's likeness to an array. Columns are never typed.
|
25
29
|
def initialize filename, *columns
|
26
30
|
raise ArgumentError, 'expecting column names' if columns.empty?
|
27
31
|
|
@@ -52,6 +56,8 @@ class Turntable
|
|
52
56
|
end
|
53
57
|
|
54
58
|
# Allows you to access Turntable rows and/or metadata like an array or hash.
|
59
|
+
# If index is an integer, you're looking for a row of data. If index is
|
60
|
+
# something else (usually a symbol), you're looking for a piece metadata.
|
55
61
|
def [](index)
|
56
62
|
@table[index]
|
57
63
|
end
|
@@ -62,7 +68,7 @@ class Turntable
|
|
62
68
|
end
|
63
69
|
|
64
70
|
# Inserts a new column. Fills all of the pre-existing rows with nil. Returns
|
65
|
-
# the new
|
71
|
+
# the new columns list.
|
66
72
|
def add_column column_name
|
67
73
|
raise ArgumentError, "column #{column_name} already exists" if @table[:columns].include?(column_name)
|
68
74
|
|
@@ -81,18 +87,19 @@ class Turntable
|
|
81
87
|
alias :all :all_numbered_rows
|
82
88
|
alias :* :all_numbered_rows
|
83
89
|
|
84
|
-
# Removes a given row. Returns the
|
90
|
+
# Removes a given row. Returns the changed database.
|
85
91
|
def delete id
|
86
92
|
@table.delete id
|
87
93
|
self.update
|
88
94
|
@table
|
89
95
|
end
|
90
96
|
|
91
|
-
# A
|
97
|
+
# A leaner, meaner inspect allows dealing with Turntable in IRB to be much
|
98
|
+
# easier.
|
92
99
|
def inspect; self.to_s; end
|
93
100
|
|
94
|
-
# Returns a boolean indicating whether
|
95
|
-
# data.
|
101
|
+
# Returns a boolean indicating whether an equivalent select statement would
|
102
|
+
# return any data.
|
96
103
|
def include?
|
97
104
|
result = []
|
98
105
|
self.each { |row| result.push row if yield row }
|
@@ -107,7 +114,9 @@ class Turntable
|
|
107
114
|
end
|
108
115
|
|
109
116
|
# Pushes self with a new row. Accepts either a list of arguments, or one
|
110
|
-
# array which represents the same thing.
|
117
|
+
# array which represents the same thing. The number of arguments must equal
|
118
|
+
# the number of columns in the database. This is equivalent to the SQL INSERT
|
119
|
+
# statement.
|
111
120
|
def push *args
|
112
121
|
args = args[0] if args.length == 1 and args[0].is_a?(Array)
|
113
122
|
|
@@ -133,8 +142,9 @@ class Turntable
|
|
133
142
|
# Just a wrapper for Array#to_textile.
|
134
143
|
def to_textile; self.to_a.to_textile; end
|
135
144
|
|
136
|
-
#
|
137
|
-
#
|
145
|
+
# Updates self and the Turntable file, by marshalling self to the file. Any
|
146
|
+
# changes to the external file must occur within this block. If something
|
147
|
+
# goes wrong, like trying to push a Proc object, then no changes are made.
|
138
148
|
def update
|
139
149
|
@pushing ? (@before_table) : (@before_table = @table.dup)
|
140
150
|
what_happened = yield if block_given?
|
@@ -157,7 +167,9 @@ class Turntable
|
|
157
167
|
|
158
168
|
end
|
159
169
|
|
160
|
-
# Rows are custom OpenStructs, made with dictionaries
|
170
|
+
# Rows are custom OpenStructs, made with dictionaries instead of hashes. Rows
|
171
|
+
# may be created only in this manner, unlike regular OpenStructs, which also
|
172
|
+
# accept a hash of values at creation.
|
161
173
|
class Row < OpenStruct
|
162
174
|
def initialize; @table = Dictionary.new; end
|
163
175
|
public :table
|
@@ -181,7 +193,7 @@ class Array
|
|
181
193
|
|
182
194
|
string += '</table>'
|
183
195
|
end
|
184
|
-
|
196
|
+
|
185
197
|
# Writes the output of any query to STDOUT.
|
186
198
|
def to_stdout; puts self.to_textile; end
|
187
199
|
|
@@ -204,10 +216,11 @@ end
|
|
204
216
|
|
205
217
|
class Object
|
206
218
|
|
207
|
-
#
|
219
|
+
# Turntable uses id to identify a database's rows. Undefining this method
|
220
|
+
# turns off Object#id deprecation warnings.
|
208
221
|
undef id
|
209
222
|
|
210
|
-
# Just like regular puts
|
223
|
+
# Just like regular puts, except putting a Turntable calls
|
211
224
|
# Turntable#to_textile.
|
212
225
|
def puts obj
|
213
226
|
super obj
|
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: 1.1.
|
4
|
+
version: 1.1.2
|
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-
|
12
|
+
date: 2009-07-16 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -25,7 +25,7 @@ files:
|
|
25
25
|
- turntable.rb
|
26
26
|
- dictionary.rb
|
27
27
|
has_rdoc: false
|
28
|
-
homepage: http://github.com/
|
28
|
+
homepage: http://christiank.github.com/Turntable/
|
29
29
|
post_install_message:
|
30
30
|
rdoc_options: []
|
31
31
|
|