• R/O
  • SSH
  • HTTPS

tenarai: 提交


Commit MetaInfo

修訂57 (tree)
時間2008-03-10 22:28:28
作者junkikuchi

Log Message

updated.

Change Summary

差異

--- trunk/test/test_table.rb (revision 56)
+++ trunk/test/test_table.rb (revision 57)
@@ -7,6 +7,7 @@
77
88 class DBTableTest < Test::Unit::TestCase
99 class A < Tenarai::DB::Row
10+ column Tenarai::DB::Boolean.new('b1', :default => false)
1011 column Tenarai::DB::Integer.new('i1', :default => 100)
1112 column Tenarai::DB::Integer.new('i2', :unsigned => true)
1213 column Tenarai::DB::String.new('s1', :default => 'DEFAULT')
@@ -487,6 +488,7 @@
487488 assert_equal(100, a.i1)
488489 assert_equal('DEFAULT', a.s1)
489490
491+ a.b1 = true
490492 a.i1 = 1
491493 a.i2 = 2
492494 a.s1 = 'a'
@@ -498,6 +500,7 @@
498500 a.save
499501
500502 assert_equal(1, a.id)
503+ assert_equal(true, a.b1)
501504 assert_equal(1, a.i1)
502505 assert_equal(2, a.i2)
503506 assert_equal('a', a.s1)
@@ -508,6 +511,7 @@
508511
509512 time = Time.now
510513
514+ a.b1 = false
511515 a.i1 = 10
512516 a.i2 = 20
513517 a.s1 = 'A'
@@ -519,6 +523,7 @@
519523 a.save
520524
521525 assert_equal(1, a.id)
526+ assert_equal(false, a.b1)
522527 assert_equal(10, a.i1)
523528 assert_equal(20, a.i2)
524529 assert_equal('A', a.s1)
@@ -534,6 +539,7 @@
534539 a = table.fetch(1)
535540
536541 assert_equal(1, a.id)
542+ assert_equal(false, a.b1)
537543 assert_equal(10, a.i1)
538544 assert_equal(20, a.i2)
539545 assert_equal('A', a.s1)
@@ -553,6 +559,7 @@
553559 a = table.fetch(1)
554560
555561 assert_equal(1, a.id)
562+ assert_equal(false, a.b1)
556563 assert_equal(10, a.i1)
557564 assert_equal(20, a.i2)
558565 assert_equal('A', a.s1)
@@ -564,6 +571,7 @@
564571 a.delete
565572
566573 assert_nil(a.id)
574+ assert_equal(false, a.b1)
567575 assert_equal(10, a.i1)
568576 assert_equal(20, a.i2)
569577 assert_equal('A', a.s1)
--- trunk/Rakefile (revision 56)
+++ trunk/Rakefile (revision 57)
@@ -34,7 +34,7 @@
3434 :class => 'User',
3535 :row => {
3636 'name' => 'admin',
37- '_password' => Tenarai::ClassLoader['User'].digest('admin')
37+ '_password' => Tenarai::ClassLoader['User'].digest('admin'),
3838 },
3939 :relation => {
4040 'content' => 'content:admin',
--- trunk/lib/tenarai/db/column.rb (revision 56)
+++ trunk/lib/tenarai/db/column.rb (revision 57)
@@ -9,7 +9,7 @@
99 attr_reader :param
1010
1111 def initialize(name, param={})
12- @name = name
12+ @name = name
1313 @param = param
1414 end
1515
@@ -37,6 +37,12 @@
3737 end
3838 end
3939
40+ class Boolean < Column
41+ def execute(command)
42+ command.boolean(self)
43+ end
44+ end
45+
4046 class Integer < Column
4147 def execute(command)
4248 if @param[:unsigned]
--- trunk/lib/tenarai/db/preference.rb (revision 56)
+++ trunk/lib/tenarai/db/preference.rb (revision 57)
@@ -16,7 +16,7 @@
1616 class Engine
1717 module Load
1818 def preference(col)
19- @handlers[col.name] = lambda do |row, val|
19+ @handler[col.name] = lambda do |row, val|
2020 row.class.meta[:preference].load_param(val)
2121 end
2222 end
@@ -25,7 +25,7 @@
2525 module Dump
2626 def preference(col)
2727 super
28- @handlers[col.name] = lambda do |row, val|
28+ @handler[col.name] = lambda do |row, val|
2929 val.dump_param(row.class.meta[:preference])
3030 end
3131 end
--- trunk/lib/tenarai/db/engine.rb (revision 56)
+++ trunk/lib/tenarai/db/engine.rb (revision 57)
@@ -8,7 +8,7 @@
88 module Load
99 def initialize(engine,table)
1010 super
11- @handlers = {}
11+ @handler = {}
1212 end
1313
1414 def build
@@ -17,14 +17,18 @@
1717
1818 def handler(col)
1919 if default = col.param[:default]
20- @handlers[col.name] = lambda do |row, val|
21- val || default
20+ @handler[col.name] = lambda do |row, val|
21+ if val.nil?
22+ default
23+ else
24+ val
25+ end
2226 end
2327 end
2428 end
2529
2630 def date(col)
27- @handlers[col.name] = lambda do |row, val|
31+ @handler[col.name] = lambda do |row, val|
2832 if val
2933 Time.local(
3034 val.year, val.month, val.day, val.hour, val.minute, val.second
@@ -36,7 +40,7 @@
3640 end
3741
3842 def serialize(col)
39- @handlers[col.name] = lambda do |row, val|
43+ @handler[col.name] = lambda do |row, val|
4044 if val
4145 Marshal.load(val)
4246 else
@@ -46,13 +50,13 @@
4650 end
4751
4852 def multiple(ref)
49- @handlers[ref.name] = lambda do |row, val|
53+ @handler[ref.name] = lambda do |row, val|
5054 @table.relation[ref.name].create_multiple(ref, row, val)
5155 end
5256 end
5357
5458 def single(ref)
55- @handlers[ref.name] = lambda do |row, val|
59+ @handler[ref.name] = lambda do |row, val|
5660 @table.relation[ref.name].create_single(ref, row, val)
5761 end
5862 end
@@ -60,7 +64,7 @@
6064 def execute(row)
6165 row.primary_key = row.row.delete(row.class.meta[:primary_key].name)
6266 row.row[row.class.meta[:class_name].name] = row.class.name
63- @handlers.each do |key, val|
67+ @handler.each do |key, val|
6468 row.row[key] = val.call(row, row.row[key])
6569 end
6670 end
@@ -69,42 +73,42 @@
6973 module Dump
7074 def initialize(engine, table)
7175 super
72- @cols = []
73- @handlers = {}
74- @handlers.default = lambda do |row, val| val end
76+ @col = []
77+ @handler = {}
78+ @handler.default = lambda do |row, val| val end
7579 end
7680
7781 def build
7882 each_class_name do |val| dispatch(val) end
79- each_column do |val| dispatch(val) end
83+ each_column do |val| dispatch(val) end
8084 end
8185
8286 def handler(col)
83- @cols << col.name
87+ @col << col.name
8488 end
8589
8690 def serialize(col)
8791 super
88- @handlers[col.name] = lambda do |row, val|
92+ @handler[col.name] = lambda do |row, val|
8993 Marshal.dump(val)
9094 end
9195 end
9296
9397 def multiple(col)
94- @handlers[col.name] = lambda do |row, val|
98+ @handler[col.name] = lambda do |row, val|
9599 end
96100 end
97101
98102 def single(col)
99103 super
100- @handlers[col.name] = lambda do |row, val|
104+ @handler[col.name] = lambda do |row, val|
101105 val && val.primary_key
102106 end
103107 end
104108
105109 def build_bindvars(row)
106- @cols.map do |key|
107- @handlers[key].call(row, row.row[key])
110+ @col.map do |key|
111+ @handler[key].call(row, row.row[key])
108112 end
109113 end
110114 end
@@ -111,10 +115,10 @@
111115
112116 class Command
113117 def initialize(engine, table)
114- @engine = engine
115- @table = table
116- @stmt = ''
117- @bindvars = []
118+ @engine = engine
119+ @table = table
120+ @stmt = ''
121+ @bindvar = []
118122 end
119123
120124 def each_primary_key(&block)
@@ -150,7 +154,7 @@
150154 end
151155
152156 def execute(*param)
153- @table.db.execute(@stmt, *@bindvars)
157+ @table.db.execute(@stmt, *@bindvar)
154158 end
155159
156160 def handler(col)
@@ -157,6 +161,7 @@
157161 end
158162
159163 def primary_key(col) handler(col); end
164+ def boolean(col) handler(col); end
160165 def integer(col) handler(col); end
161166 def integer_unsigned(col) handler(col); end
162167 def varchar(col) handler(col); end
@@ -174,7 +179,7 @@
174179 class CreateTable < Command
175180 def initialize(engine, table)
176181 super
177- @cols = []
182+ @col = []
178183 @idxs = []
179184 end
180185
@@ -181,43 +186,47 @@
181186 def build
182187 each_primary_key do |val| dispatch(val) end
183188 each_class_name do |val| dispatch(val) end
184- each_column do |val| dispatch(val) end
185- each_index do |val| dispatch(val) end
186- @stmt = self.class::SQL % [@table.name, (@cols + @idxs).join(",\n")]
189+ each_column do |val| dispatch(val) end
190+ each_index do |val| dispatch(val) end
191+ @stmt = self.class::SQL % [@table.name, (@col + @idxs).join(",\n")]
187192 end
188193
189194 def primary_key(col)
190- @cols << self.class::SQL_PRIMARY_KEY % col.name
195+ @col << self.class::SQL_PRIMARY_KEY % col.name
191196 end
192197
198+ def boolean(col)
199+ @col << self.class::SQL_BOOLEAN % col.name
200+ end
201+
193202 def integer(col)
194- @cols << self.class::SQL_INT % col.name
203+ @col << self.class::SQL_INT % col.name
195204 end
196205
197206 def integer_unsigned(col)
198- @cols << self.class::SQL_INT_UNSIG % col.name
207+ @col << self.class::SQL_INT_UNSIG % col.name
199208 end
200209
201210 def varchar(col)
202- @cols << self.class::SQL_VARCHAR % [col.name, col.param[:length]]
211+ @col << self.class::SQL_VARCHAR % [col.name, col.param[:length]]
203212 end
204213
205214 def char(col)
206- @cols << self.class::SQL_CHAR % [col.name, col.param[:length]]
215+ @col << self.class::SQL_CHAR % [col.name, col.param[:length]]
207216 end
208217
209218 def text(col)
210- @cols << self.class::SQL_TEXT % col.name
219+ @col << self.class::SQL_TEXT % col.name
211220 end
212221
213222 def date(col)
214- @cols << self.class::SQL_DATE % col.name
223+ @col << self.class::SQL_DATE % col.name
215224 end
216225
217226 alias :serialize :text
218227
219228 def class_name(col)
220- @cols << self.class::SQL_VARCHAR % [col.name, 255]
229+ @col << self.class::SQL_VARCHAR % [col.name, 255]
221230 end
222231
223232 def multiple(col)
@@ -224,7 +233,7 @@
224233 end
225234
226235 def single(col)
227- @cols << self.class::SQL_INT_UNSIG % col.name
236+ @col << self.class::SQL_INT_UNSIG % col.name
228237 @idxs << self.class::SQL_INDEX % [col.name, col.name]
229238 end
230239
@@ -264,7 +273,7 @@
264273
265274 def execute(col, row)
266275 @stmt = self.class::SQL % [@table.name, col, col]
267- @bindvars = row.primary_key
276+ @bindvar = row.primary_key
268277 super
269278 @engine.rows.each do |key, val|
270279 val.row[col] == row && val.row[col] = nil
@@ -280,7 +289,7 @@
280289 def build
281290 super
282291 @stmt = self.class::SQL % [
283- @table.name, @cols.join(', '), (['?'] * @cols.length).join(', ')
292+ @table.name, @col.join(', '), (['?'] * @col.length).join(', ')
284293 ]
285294 end
286295
@@ -288,7 +297,7 @@
288297 end
289298
290299 def execute(row)
291- @bindvars = build_bindvars(row)
300+ @bindvar = build_bindvars(row)
292301 super
293302 row.primary_key = primary_key
294303 @engine.rows[row.primary_key] = row
@@ -304,14 +313,14 @@
304313 super
305314 @stmt = self.class::SQL % [
306315 @table.name,
307- @cols.map do |key| '%s = ?' % key end.join(', '),
316+ @col.map do |key| '%s = ?' % key end.join(', '),
308317 @table.row_class.meta[:primary_key].name
309318 ]
310319 end
311320
312321 def execute(row)
313- @bindvars = build_bindvars(row)
314- @bindvars.push(row.primary_key)
322+ @bindvar = build_bindvars(row)
323+ @bindvar.push(row.primary_key)
315324 super
316325 end
317326 end
@@ -347,9 +356,9 @@
347356
348357 def execute(key)
349358 key = key.to_i
350- @bindvars = [key]
359+ @bindvar = [key]
351360 unless @engine.rows.key?(key)
352- @table.db.query(@stmt, *@bindvars) do |val|
361+ @table.db.query(@stmt, *@bindvar) do |val|
353362 @engine.rows[key] ||= @table.create_row(val)
354363 end
355364 end
@@ -378,7 +387,7 @@
378387
379388 def initialize(engine, table)
380389 super
381- @handlers = {}
390+ @handler = {}
382391 end
383392
384393 def build
@@ -389,8 +398,8 @@
389398 end
390399
391400 def execute(row)
392- @handlers.each do |key, val| val.call(row, key) end
393- @bindvars = [row.primary_key]
401+ @handler.each do |key, val| val.call(row, key) end
402+ @bindvar = [row.primary_key]
394403 super
395404 @engine.rows.delete(row.primary_key)
396405 row.primary_key = nil
@@ -397,7 +406,7 @@
397406 end
398407
399408 def multiple(ref)
400- @handlers[ref.name] = lambda do |row, key|
409+ @handler[ref.name] = lambda do |row, key|
401410 @table.relation[key].unlink(row)
402411 end
403412 end
@@ -421,8 +430,8 @@
421430 SQL = "DELETE FROM %s "
422431
423432 def execute(condition, *bindvars)
424- @stmt = self.class::SQL % @table.name + condition
425- @bindvars = *bindvars
433+ @stmt = self.class::SQL % @table.name + condition
434+ @bindvar = *bindvars
426435 super
427436 @engine.reload
428437 end
@@ -433,7 +442,7 @@
433442 def initialize(table)
434443 @rows = {}
435444 @table = table
436- @commands = {}
445+ @command = {}
437446 @command_map = {
438447 :create_table => self.class::CreateTable,
439448 :drop_table => self.class::DropTable,
@@ -456,16 +465,16 @@
456465 end
457466
458467 def build
459- @commands = @command_map.inject({}) do |ret, (key, val)|
468+ @command = @command_map.inject({}) do |ret, (key, val)|
460469 instance_eval <<-END
461470 def #{key}(*param, &block)
462- @commands[:#{key}].execute(*param, &block)
471+ @command[:#{key}].execute(*param, &block)
463472 end
464473 END
465474 ret[key] = val.new(self, @table)
466475 ret
467476 end
468- @commands.each do |key, val| val.build end
477+ @command.each do |key, val| val.build end
469478 end
470479
471480 def refresh
--- trunk/lib/tenarai/db/mysql.rb (revision 56)
+++ trunk/lib/tenarai/db/mysql.rb (revision 57)
@@ -10,6 +10,7 @@
1010 class CreateTable < CreateTable
1111 SQL = "CREATE TABLE %s (\n%s\n) TYPE=INNODB"
1212 SQL_PRIMARY_KEY = ' %s INT UNSIGNED PRIMARY KEY AUTO_INCREMENT'
13+ SQL_BOOLEAN = ' %s BOOLEAN'
1314 SQL_INT = ' %s INT'
1415 SQL_INT_UNSIG = ' %s INT UNSIGNED'
1516 SQL_VARCHAR = ' %s VARCHAR(%s)'
--- trunk/main.rb (revision 56)
+++ trunk/main.rb (revision 57)
@@ -143,4 +143,4 @@
143143 Main.new(CONFIG).mongrel
144144 when 'webrick'
145145 Main.new(CONFIG).webrick
146-end
146+end if __FILE__ == $0
--- trunk/webapp/User.rb (revision 56)
+++ trunk/webapp/User.rb (revision 57)
@@ -20,6 +20,7 @@
2020 end
2121 end
2222
23+ column Tenarai::DB::Boolean.new('active', :default => true)
2324 column Tenarai::DB::String.new('name', :length => 255)
2425 column Tenarai::DB::String.new('_password', :length => 255)
2526 column Tenarai::DB::Reference.new('content')
@@ -30,6 +31,7 @@
3031 end
3132
3233 def auth(password)
33- self._password == self.class.digest(password)
34+ p [name, self.active]
35+ active && self._password == self.class.digest(password)
3436 end
3537 end
Show on old repository browser