baza 0.0.14 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +58 -13
  3. data/VERSION +1 -1
  4. data/baza.gemspec +15 -3
  5. data/include/db.rb +871 -865
  6. data/include/drivers/mysql/mysql.rb +104 -297
  7. data/include/drivers/mysql/mysql_column.rb +133 -0
  8. data/include/drivers/mysql/mysql_columns.rb +4 -127
  9. data/include/drivers/mysql/mysql_index.rb +76 -0
  10. data/include/drivers/mysql/mysql_indexes.rb +0 -73
  11. data/include/drivers/mysql/mysql_result.rb +42 -0
  12. data/include/drivers/mysql/mysql_result_java.rb +61 -0
  13. data/include/drivers/mysql/mysql_result_mysql2.rb +26 -0
  14. data/include/drivers/mysql/mysql_result_unbuffered.rb +72 -0
  15. data/include/drivers/mysql/mysql_sqlspecs.rb +1 -1
  16. data/include/drivers/mysql/mysql_table.rb +361 -0
  17. data/include/drivers/mysql/mysql_tables.rb +23 -381
  18. data/include/drivers/sqlite3/libknjdb_java_sqlite3.rb +17 -22
  19. data/include/drivers/sqlite3/libknjdb_sqlite3_ironruby.rb +13 -13
  20. data/include/drivers/sqlite3/sqlite3.rb +39 -105
  21. data/include/drivers/sqlite3/sqlite3_column.rb +146 -0
  22. data/include/drivers/sqlite3/sqlite3_columns.rb +17 -149
  23. data/include/drivers/sqlite3/sqlite3_index.rb +55 -0
  24. data/include/drivers/sqlite3/sqlite3_indexes.rb +0 -52
  25. data/include/drivers/sqlite3/sqlite3_result.rb +35 -0
  26. data/include/drivers/sqlite3/sqlite3_result_java.rb +39 -0
  27. data/include/drivers/sqlite3/sqlite3_table.rb +399 -0
  28. data/include/drivers/sqlite3/sqlite3_tables.rb +7 -403
  29. data/include/idquery.rb +19 -19
  30. data/include/model.rb +139 -139
  31. data/include/model_handler_sqlhelper.rb +74 -74
  32. data/spec/support/driver_columns_collection.rb +17 -0
  33. metadata +14 -2
data/include/model.rb CHANGED
@@ -5,27 +5,27 @@
5
5
  # user = ob.get(:User, 1) #=> <Models::User> that extends <Baza::Datarow>
6
6
  class Baza::Model
7
7
  @@refs = {}
8
-
8
+
9
9
  #Returns the Baza::ModelHandler which handels this model.
10
10
  def ob
11
11
  return self.class.ob
12
12
  end
13
-
13
+
14
14
  #Returns the Baza::Db which handels this model.
15
15
  def db
16
16
  return self.class.db
17
17
  end
18
-
18
+
19
19
  #Returns the 'Baza::ModelHandler'-object that handels this class.
20
20
  def self.ob
21
21
  return @ob
22
22
  end
23
-
23
+
24
24
  #Returns the 'Baza::Db'-object that handels this class.
25
25
  def self.db
26
26
  return @db
27
27
  end
28
-
28
+
29
29
  #This is used by 'Baza::ModelHandler' to find out what data is required for this class. Returns the array that tells about required data.
30
30
  #===Examples
31
31
  #When adding a new user, this can fail if the ':group_id' is not given, or the ':group_id' doesnt refer to a valid group-row in the db.
@@ -38,7 +38,7 @@ class Baza::Model
38
38
  @required_data = [] if !@required_data
39
39
  return @required_data
40
40
  end
41
-
41
+
42
42
  #This is used by 'Baza::ModelHandler' to find out what other objects this class depends on. Returns the array that tells about depending data.
43
43
  #===Examples
44
44
  #This will tell Baza::ModelHandler that files depends on users. It can prevent the user from being deleted, if any files depend on it.
@@ -50,13 +50,13 @@ class Baza::Model
50
50
  def self.depending_data
51
51
  return @depending_data
52
52
  end
53
-
53
+
54
54
  #Returns true if this class has been initialized.
55
55
  def self.initialized?
56
56
  return false if !@columns_sqlhelper_args
57
57
  return true
58
58
  end
59
-
59
+
60
60
  #This is used by 'Baza::ModelHandler' to find out which other objects should be deleted when an object of this class is deleted automatically. Returns the array that tells about autodelete data.
61
61
  #===Examples
62
62
  #This will trigger Baza::ModelHandler to automatically delete all the users pictures, when deleting the current user.
@@ -68,19 +68,19 @@ class Baza::Model
68
68
  def self.autodelete_data
69
69
  return @autodelete_data
70
70
  end
71
-
71
+
72
72
  #Returns the autozero-data (if any).
73
73
  def self.autozero_data
74
74
  return @autozero_data
75
75
  end
76
-
76
+
77
77
  #This helps various parts of the framework determine if this is a datarow class without requiring it.
78
78
  #===Examples
79
79
  # print "This is a knj-object." if obj.respond_to?("is_knj?")
80
80
  def is_knj?
81
81
  return true
82
82
  end
83
-
83
+
84
84
  #This tests if a certain string is a date-null-stamp.
85
85
  #===Examples
86
86
  # time_str = dbrow[:date]
@@ -89,7 +89,7 @@ class Baza::Model
89
89
  return true if !stamp or stamp == "0000-00-00 00:00:00" or stamp == "0000-00-00"
90
90
  return false
91
91
  end
92
-
92
+
93
93
  #This is used to define datarows that this object can have a lot of.
94
94
  #===Examples
95
95
  #This will define the method "pictures" on 'Models::User' that will return all pictures for the users and take possible Objects-sql-arguments. It will also enabling joining pictures when doing Objects-sql-lookups.
@@ -105,7 +105,7 @@ class Baza::Model
105
105
  classname, colname, methodname = *val
106
106
  elsif val.is_a?(Hash)
107
107
  classname, colname, methodname = nil, nil, nil
108
-
108
+
109
109
  val.each do |hkey, hval|
110
110
  case hkey
111
111
  when :class
@@ -120,9 +120,9 @@ class Baza::Model
120
120
  raise "Invalid key for 'has_many': '#{hkey}'."
121
121
  end
122
122
  end
123
-
123
+
124
124
  colname = "#{self.name.to_s.split("::").last.to_s.downcase}_id".to_sym if colname.to_s.empty?
125
-
125
+
126
126
  if val[:depends]
127
127
  @depending_data = [] if !@depending_data
128
128
  @depending_data << {
@@ -130,7 +130,7 @@ class Baza::Model
130
130
  :classname => classname
131
131
  }
132
132
  end
133
-
133
+
134
134
  if val[:autodelete]
135
135
  @autodelete_data = [] if !@autodelete_data
136
136
  @autodelete_data << {
@@ -138,7 +138,7 @@ class Baza::Model
138
138
  :classname => classname
139
139
  }
140
140
  end
141
-
141
+
142
142
  if val[:autozero]
143
143
  @autozero_data = [] if !@autozero_data
144
144
  @autozero_data << {
@@ -149,19 +149,19 @@ class Baza::Model
149
149
  else
150
150
  raise "Unknown argument: '#{val.class.name}'."
151
151
  end
152
-
152
+
153
153
  raise "No classname given." if !classname
154
154
  methodname = "#{StringCases.camel_to_snake(classname)}s".to_sym if !methodname
155
155
  raise "No column was given for '#{self.name}' regarding has-many-class: '#{classname}'." if !colname
156
-
156
+
157
157
  if val.is_a?(Hash) and val.key?(:where)
158
158
  where_args = val[:where]
159
159
  else
160
160
  where_args = nil
161
161
  end
162
-
162
+
163
163
  self.define_many_methods(classname, methodname, colname, where_args)
164
-
164
+
165
165
  self.joined_tables(
166
166
  classname => {
167
167
  :where => {
@@ -171,26 +171,26 @@ class Baza::Model
171
171
  )
172
172
  end
173
173
  end
174
-
174
+
175
175
  #This define is this object has one element of another datarow-class. It define various methods and joins based on that.
176
176
  #===Examples
177
177
  # class Models::User < Baza::Datarow
178
178
  # has_one [
179
179
  # #Defines the method 'group', which returns a 'Group'-object by the column 'group_id'.
180
180
  # :Group,
181
- #
181
+ #
182
182
  # #Defines the method 'type', which returns a 'Type'-object by the column 'type_id'.
183
183
  # {:class => :Type, :col => :type_id, :method => :type}
184
184
  # ]
185
185
  # end
186
186
  def self.has_one(arr)
187
187
  arr = [arr] if arr.is_a?(Symbol)
188
-
188
+
189
189
  arr.each do |val|
190
190
  methodname = nil
191
191
  colname = nil
192
192
  classname = nil
193
-
193
+
194
194
  if val.is_a?(Symbol)
195
195
  classname = val
196
196
  methodname = val.to_s.downcase.to_sym
@@ -199,7 +199,7 @@ class Baza::Model
199
199
  classname, colname, methodname = *val
200
200
  elsif val.is_a?(Hash)
201
201
  classname, colname, methodname = nil, nil, nil
202
-
202
+
203
203
  val.each do |hkey, hval|
204
204
  case hkey
205
205
  when :class
@@ -214,7 +214,7 @@ class Baza::Model
214
214
  raise "Invalid key for class '#{self.name}' functionality 'has_many': '#{hkey}'."
215
215
  end
216
216
  end
217
-
217
+
218
218
  if val[:required]
219
219
  colname = "#{classname.to_s.downcase}_id".to_sym if !colname
220
220
  self.required_data << {
@@ -225,11 +225,11 @@ class Baza::Model
225
225
  else
226
226
  raise "Unknown argument-type: '#{arr.class.name}'."
227
227
  end
228
-
228
+
229
229
  methodname = StringCases.camel_to_snake(classname) if !methodname
230
230
  colname = "#{classname.to_s.downcase}_id".to_sym if !colname
231
231
  self.define_one_methods(classname, methodname, colname)
232
-
232
+
233
233
  self.joined_tables(
234
234
  classname => {
235
235
  :where => {
@@ -239,79 +239,79 @@ class Baza::Model
239
239
  )
240
240
  end
241
241
  end
242
-
242
+
243
243
  #This method initializes joins, sets methods to update translations and makes the translations automatically be deleted when the object is deleted.
244
244
  #===Examples
245
245
  # class Models::Article < Baza::Datarow
246
246
  # #Defines methods such as: 'title', 'title=', 'content', 'content='. When used with Knjappserver these methods will change what they return and set based on the current language of the session.
247
247
  # has_translation [:title, :content]
248
248
  # end
249
- #
249
+ #
250
250
  # article = ob.get(:Article, 1)
251
251
  # print "The title in the current language is: '#{article.title}'."
252
- #
252
+ #
253
253
  # article.title = 'Title in english if the language is english'
254
254
  def self.has_translation(arr)
255
255
  @translations = [] if !@translations
256
-
256
+
257
257
  arr.each do |val|
258
258
  @translations << val
259
-
259
+
260
260
  val_dc = val.to_s.downcase
261
261
  table_name = "Translation_#{val_dc}".to_sym
262
-
262
+
263
263
  joined_tables(
264
264
  table_name => {
265
- :where => {
265
+ where: {
266
266
  "object_class" => self.name,
267
- "object_id" => {:type => :col, :name => :id},
267
+ "object_id" => {type: :col, name: :id},
268
268
  "key" => val.to_s,
269
269
  "locale" => proc{|d| _session[:locale]}
270
270
  },
271
- :parent_table => :Translation,
272
- :datarow => Knj::Translations::Translation,
273
- :ob => @ob
271
+ parent_table: :Translation,
272
+ datarow: Knj::Translations::Translation,
273
+ ob: @ob
274
274
  }
275
275
  )
276
-
277
- self.define_translation_methods(:val => val, :val_dc => val_dc)
276
+
277
+ self.define_translation_methods(val: val, val_dc: val_dc)
278
278
  end
279
279
  end
280
-
280
+
281
281
  #This returns all translations for this datarow-class.
282
282
  def self.translations
283
283
  return @translations
284
284
  end
285
-
285
+
286
286
  #Returns data about joined tables for this class.
287
287
  def self.joined_tables(hash)
288
288
  @columns_joined_tables = {} if !@columns_joined_tables
289
289
  @columns_joined_tables.merge!(hash)
290
290
  end
291
-
291
+
292
292
  #Returns various data for the objects-sql-helper. This can be used to view various informations about the columns and more.
293
293
  def self.columns_sqlhelper_args
294
294
  raise "No SQLHelper arguments has been spawned yet." if !@columns_sqlhelper_args
295
295
  return @columns_sqlhelper_args
296
296
  end
297
-
297
+
298
298
  #Called by Baza::ModelHandler to initialize the model and load column-data on-the-fly.
299
299
  def self.load_columns(d)
300
300
  @ob = d.ob
301
301
  @db = d.db
302
-
302
+
303
303
  @classname = self.name.split("::").last.to_sym if !@classname
304
304
  @table = @classname if !@table
305
305
  @mutex = Monitor.new if !@mutex
306
-
306
+
307
307
  #Cache these to avoid method-lookups.
308
308
  @sep_col = @db.sep_col
309
309
  @sep_table = @db.sep_table
310
310
  @table_str = "#{@sep_table}#{@db.esc_table(@table)}#{@sep_table}"
311
-
311
+
312
312
  @mutex.synchronize do
313
313
  inst_methods = self.instance_methods(false)
314
-
314
+
315
315
  sqlhelper_args = {
316
316
  :db => @db,
317
317
  :table => @table,
@@ -322,18 +322,18 @@ class Baza::Model
322
322
  :cols_str => [],
323
323
  :cols => {}
324
324
  }
325
-
325
+
326
326
  sqlhelper_args[:table] = @table
327
-
327
+
328
328
  @db.tables[table].columns do |col_obj|
329
329
  col_name = col_obj.name.to_s
330
330
  col_name_sym = col_name.to_sym
331
331
  col_type = col_obj.type
332
332
  col_type = :int if col_type == :bigint or col_type == :tinyint or col_type == :mediumint or col_type == :smallint
333
333
  sqlhelper_args[:cols][col_name] = true
334
-
334
+
335
335
  self.define_bool_methods(inst_methods, col_name)
336
-
336
+
337
337
  if col_type == :enum and col_obj.maxlength == "'0','1'"
338
338
  sqlhelper_args[:cols_bools] << col_name
339
339
  elsif col_type == :int and col_name.slice(-3, 3) == "_id"
@@ -346,49 +346,49 @@ class Baza::Model
346
346
  sqlhelper_args[:cols_date] << col_name
347
347
  self.define_date_methods(inst_methods, col_name_sym)
348
348
  end
349
-
349
+
350
350
  if col_type == :int or col_type == :decimal
351
351
  self.define_numeric_methods(inst_methods, col_name_sym)
352
352
  end
353
-
353
+
354
354
  if col_type == :int or col_type == :varchar
355
355
  self.define_text_methods(inst_methods, col_name_sym)
356
356
  end
357
-
357
+
358
358
  if col_type == :time
359
359
  self.define_time_methods(inst_methods, col_name_sym)
360
360
  end
361
361
  end
362
-
362
+
363
363
  if @columns_joined_tables
364
364
  @columns_joined_tables.each do |table_name, table_data|
365
365
  table_data[:where].each do |key, val|
366
366
  val[:table] = @table if val.is_a?(Hash) and !val.key?(:table) and val[:type].to_sym == :col
367
367
  end
368
-
368
+
369
369
  table_data[:datarow] = @ob.args[:module].const_get(table_name.to_sym) if !table_data.key?(:datarow)
370
370
  end
371
-
371
+
372
372
  sqlhelper_args[:joined_tables] = @columns_joined_tables
373
373
  end
374
-
374
+
375
375
  @columns_sqlhelper_args = sqlhelper_args
376
376
  end
377
-
377
+
378
378
  self.init_class(d) if self.respond_to?(:init_class)
379
379
  end
380
-
380
+
381
381
  #This method helps returning objects and supports various arguments. It should be called by Object#list.
382
382
  #===Examples
383
383
  # ob.list(:User, {"username_lower" => "john doe"}) do |user|
384
384
  # print user.id
385
385
  # end
386
- #
386
+ #
387
387
  # array = ob.list(:User, {"id" => 1})
388
388
  # print array.length
389
389
  def self.list(d, &block)
390
390
  args = d.args
391
-
391
+
392
392
  if args["count"]
393
393
  count = true
394
394
  args.delete("count")
@@ -400,15 +400,15 @@ class Baza::Model
400
400
  else
401
401
  sql = "SELECT #{@table_str}.*"
402
402
  end
403
-
403
+
404
404
  qargs = nil
405
405
  ret = self.list_helper(d)
406
-
406
+
407
407
  sql << " FROM #{@table_str}"
408
408
  sql << ret[:sql_joins]
409
409
  sql << " WHERE 1=1"
410
410
  sql << ret[:sql_where]
411
-
411
+
412
412
  args.each do |key, val|
413
413
  case key
414
414
  when "return_sql"
@@ -419,36 +419,36 @@ class Baza::Model
419
419
  raise "Invalid key: '#{key}' for '#{self.name}'. Valid keys are: '#{@columns_sqlhelper_args[:cols].keys.sort}'. Date-keys: '#{@columns_sqlhelper_args[:cols_date]}'."
420
420
  end
421
421
  end
422
-
422
+
423
423
  #The count will bug if there is a group-by-statement.
424
424
  grp_shown = false
425
425
  if !count and !ret[:sql_groupby]
426
426
  sql << " GROUP BY #{@table_str}.#{@sep_col}id#{@sep_col}"
427
427
  grp_shown = true
428
428
  end
429
-
429
+
430
430
  if ret[:sql_groupby]
431
431
  if !grp_shown
432
432
  sql << " GROUP BY"
433
433
  else
434
434
  sql << ", "
435
435
  end
436
-
436
+
437
437
  sql << ret[:sql_groupby]
438
438
  end
439
-
439
+
440
440
  sql << ret[:sql_order]
441
441
  sql << ret[:sql_limit]
442
-
442
+
443
443
  return sql.to_s if args["return_sql"]
444
-
444
+
445
445
  if select_col_as_array
446
446
  enum = Enumerator.new do |yielder|
447
447
  @db.q(sql, qargs) do |data|
448
448
  yielder << data[:id]
449
449
  end
450
450
  end
451
-
451
+
452
452
  if block
453
453
  enum.each(&block)
454
454
  return nil
@@ -462,27 +462,27 @@ class Baza::Model
462
462
  return ret[:count].to_i if ret
463
463
  return 0
464
464
  end
465
-
465
+
466
466
  return @ob.list_bysql(self.classname, sql, qargs, &block)
467
467
  end
468
-
468
+
469
469
  #Helps call 'sqlhelper' on Baza::ModelHandler to generate SQL-strings.
470
470
  def self.list_helper(d)
471
471
  self.load_columns(d) if !@columns_sqlhelper_args
472
472
  @columns_sqlhelper_args[:table] = @table
473
473
  return @ob.sqlhelper(d.args, @columns_sqlhelper_args)
474
474
  end
475
-
475
+
476
476
  #Returns the classname of the object without any subclasses.
477
477
  def self.classname
478
478
  return @classname
479
479
  end
480
-
480
+
481
481
  #Sets the classname to something specific in order to hack the behaviour.
482
482
  def self.classname=(newclassname)
483
483
  @classname = newclassname
484
484
  end
485
-
485
+
486
486
  #Returns the table-name that should be used for this datarow.
487
487
  #===Examples
488
488
  # db.query("SELECT * FROM `#{Models::User.table}` WHERE username = 'John Doe'") do |data|
@@ -491,7 +491,7 @@ class Baza::Model
491
491
  def self.table
492
492
  return @table
493
493
  end
494
-
494
+
495
495
  #This can be used to manually set the table-name. Useful when meta-programming classes that extends the datarow-class.
496
496
  #===Examples
497
497
  # Models::User.table = "prefix_User"
@@ -499,7 +499,7 @@ class Baza::Model
499
499
  @table = newtable
500
500
  @columns_sqlhelper_args[:table] = @table if @columns_sqlhelper_args.is_a?(Hash)
501
501
  end
502
-
502
+
503
503
  #Returns the class-name but without having to call the class-table-method. To make code look shorter.
504
504
  #===Examples
505
505
  # user = ob.get_by(:User, {:username => 'John Doe'})
@@ -509,7 +509,7 @@ class Baza::Model
509
509
  def table
510
510
  return self.class.table
511
511
  end
512
-
512
+
513
513
  #Initializes the object. This should be called from 'Baza::ModelHandler' and not manually.
514
514
  #===Examples
515
515
  # user = ob.get(:User, 3)
@@ -519,7 +519,7 @@ class Baza::Model
519
519
  @id = @data[:id].to_i
520
520
  elsif data
521
521
  @id = data.to_i
522
-
522
+
523
523
  classname = self.class.classname.to_sym
524
524
  if self.class.ob.ids_cache_should.key?(classname)
525
525
  #ID caching is enabled for this model - dont reload until first use.
@@ -536,13 +536,13 @@ class Baza::Model
536
536
  else
537
537
  raise ArgumentError, "Could not figure out the data from '#{data.class.name}'."
538
538
  end
539
-
539
+
540
540
  if @id.to_i <= 0
541
541
  raise "Invalid ID: '#{@id}' from '#{@data}'." if @data
542
542
  raise "Invalid ID: '#{@id}'."
543
543
  end
544
544
  end
545
-
545
+
546
546
  #Reloads the data from the database.
547
547
  #===Examples
548
548
  # old_username = user[:username]
@@ -553,7 +553,7 @@ class Baza::Model
553
553
  raise Errno::ENOENT, "Could not find any data for the object with ID: '#{@id}' in the table '#{self.class.table}'." if !@data
554
554
  @should_reload = false
555
555
  end
556
-
556
+
557
557
  #Tells the object that it should reloads its data because it has changed. It wont reload before it is required though, which may save you a couple of SQL-calls.
558
558
  #===Examples
559
559
  # obj = _ob.get(:User, 5)
@@ -562,13 +562,13 @@ class Baza::Model
562
562
  @should_reload = true
563
563
  @data = nil
564
564
  end
565
-
565
+
566
566
  #Returns the data-hash that contains all the data from the database.
567
567
  def data
568
568
  self.reload if @should_reload
569
569
  return @data
570
570
  end
571
-
571
+
572
572
  #Writes/updates new data for the object.
573
573
  #===Examples
574
574
  # user.update(:username => 'New username', :date_changed => Time.now)
@@ -577,14 +577,14 @@ class Baza::Model
577
577
  self.should_reload
578
578
  self.class.ob.call("object" => self, "signal" => "update")
579
579
  end
580
-
580
+
581
581
  #Forcefully destroys the object. This is done after deleting it and should not be called manually.
582
582
  def destroy
583
583
  @id = nil
584
584
  @data = nil
585
585
  @should_reload = nil
586
586
  end
587
-
587
+
588
588
  #Returns true if that key exists on the object.
589
589
  #===Examples
590
590
  # print "Looks like the user has a name." if user.key?(:name)
@@ -593,7 +593,7 @@ class Baza::Model
593
593
  return @data.key?(key.to_sym)
594
594
  end
595
595
  alias has_key? key?
596
-
596
+
597
597
  #Returns true if the object has been deleted.
598
598
  #===Examples
599
599
  # print "That user is deleted." if user.deleted?
@@ -601,14 +601,14 @@ class Baza::Model
601
601
  return true if !@data and !@id
602
602
  return false
603
603
  end
604
-
604
+
605
605
  #Returns true if the given object no longer exists in the database. Also destroys the data on the object and sets it to deleted-status, if it no longer exists.
606
606
  #===Examples
607
607
  # print "That user is deleted." if user.deleted_from_db?
608
608
  def deleted_from_db?
609
609
  #Try to avoid db-query if object is already deleted.
610
610
  return true if self.deleted?
611
-
611
+
612
612
  #Try to reload data. Destroy object and return true if the row is gone from the database.
613
613
  begin
614
614
  self.reload
@@ -618,7 +618,7 @@ class Baza::Model
618
618
  return true
619
619
  end
620
620
  end
621
-
621
+
622
622
  #Returns a specific data from the object by key.
623
623
  # print "Username: #{user[:username]}\n"
624
624
  # print "ID: #{user[:id]}\n"
@@ -631,7 +631,7 @@ class Baza::Model
631
631
  return @data[key] if @data.key?(key)
632
632
  raise "No such key: '#{key}' on '#{self.class.name}' (#{@data.keys.join(", ")}) (#{@should_reload})."
633
633
  end
634
-
634
+
635
635
  #Writes/updates a keys value on the object.
636
636
  # user = ob.get_by(:User, {"username" => "John Doe"})
637
637
  # user[:username] = 'New username'
@@ -639,47 +639,47 @@ class Baza::Model
639
639
  self.update(key.to_sym => value)
640
640
  self.should_reload
641
641
  end
642
-
642
+
643
643
  #Returns the objects ID.
644
644
  def id
645
645
  raise Errno::ENOENT, "This object has been deleted." if self.deleted?
646
646
  raise "No ID on object." if !@id
647
647
  return @id
648
648
  end
649
-
649
+
650
650
  #This enable Wref to not return the wrong object.
651
651
  def __object_unique_id__
652
652
  return 0 if self.deleted?
653
653
  return self.id
654
654
  end
655
-
655
+
656
656
  #Tries to figure out, and returns, the possible name or title for the object.
657
657
  def name
658
658
  self.reload if @should_reload
659
-
659
+
660
660
  if @data.key?(:title)
661
661
  return @data[:title]
662
662
  elsif @data.key?(:name)
663
663
  return @data[:name]
664
664
  end
665
-
665
+
666
666
  obj_methods = self.class.instance_methods(false)
667
667
  [:name, :title].each do |method_name|
668
668
  return self.method(method_name).call if obj_methods.index(method_name)
669
669
  end
670
-
670
+
671
671
  raise "Couldnt figure out the title/name of the object on class #{self.class.name}."
672
672
  end
673
-
673
+
674
674
  #Calls the name-method and returns a HTML-escaped value. Also "[no name]" if the name is empty.
675
675
  def name_html
676
- name_str = self.name.to_s
677
- name_str = "[no name]" if name_str.length <= 0
676
+ name_str = name.to_s
677
+ name_str = "[no name]" if name_str.empty?
678
678
  return name_str
679
679
  end
680
-
680
+
681
681
  alias title name
682
-
682
+
683
683
  #Loops through the data on the object.
684
684
  #===Examples
685
685
  # user = ob.get(:User, 1)
@@ -690,25 +690,25 @@ class Baza::Model
690
690
  self.reload if @should_reload
691
691
  return @data.each(*args, &block)
692
692
  end
693
-
693
+
694
694
  #Hash-compatible.
695
695
  def to_hash
696
696
  self.reload if @should_reload
697
697
  return @data.clone
698
698
  end
699
-
699
+
700
700
  #Returns a default-URL to show the object.
701
701
  def url
702
702
  cname = self.class.classname.to_s.downcase
703
703
  return "?show=#{cname}_show&#{cname}_id=#{self.id}"
704
704
  end
705
-
705
+
706
706
  #Returns the URL for editting the object.
707
707
  def url_edit
708
708
  cname = self.class.classname.to_s.downcase
709
709
  return "?show=#{cname}_edit&#{cname}_id=#{self.id}"
710
710
  end
711
-
711
+
712
712
  #Returns the HTML for making a link to the object.
713
713
  def html(args = nil)
714
714
  if args and args[:edit]
@@ -716,12 +716,12 @@ class Baza::Model
716
716
  else
717
717
  url = self.url
718
718
  end
719
-
719
+
720
720
  return "<a href=\"#{Knj::Web.ahref_parse(url)}\">#{self.name_html}</a>"
721
721
  end
722
-
722
+
723
723
  private
724
-
724
+
725
725
  #Various methods to define methods based on the columns for the datarow.
726
726
  def self.define_translation_methods(args)
727
727
  define_method("#{args[:val_dc]}=") do |newtransval|
@@ -735,7 +735,7 @@ class Baza::Model
735
735
  })
736
736
  end
737
737
  end
738
-
738
+
739
739
  define_method("#{args[:val_dc]}") do
740
740
  begin
741
741
  return _hb.trans(self, args[:val])
@@ -743,22 +743,22 @@ class Baza::Model
743
743
  return _kas.trans(self, args[:val])
744
744
  end
745
745
  end
746
-
746
+
747
747
  define_method("#{args[:val_dc]}_html") do
748
748
  begin
749
749
  str = _hb.trans(self, args[:val])
750
750
  rescue NameError
751
751
  str = _kas.trans(self, args[:val])
752
752
  end
753
-
754
- if str.to_s.strip.length <= 0
753
+
754
+ if str.to_s.strip.empty?
755
755
  return "[no translation for #{args[:val]}]"
756
756
  end
757
-
757
+
758
758
  return str
759
759
  end
760
760
  end
761
-
761
+
762
762
  #Defines the boolean-methods based on enum-columns.
763
763
  def self.define_bool_methods(inst_methods, col_name)
764
764
  #Spawns a method on the class which returns true if the data is 1.
@@ -769,7 +769,7 @@ class Baza::Model
769
769
  end
770
770
  end
771
771
  end
772
-
772
+
773
773
  #Defines date- and time-columns based on datetime- and date-columns.
774
774
  def self.define_date_methods(inst_methods, col_name)
775
775
  if !inst_methods.include?("#{col_name}_str".to_sym)
@@ -777,11 +777,11 @@ class Baza::Model
777
777
  if Datet.is_nullstamp?(self[col_name])
778
778
  return self.class.ob.events.call(:no_date, self.class.name)
779
779
  end
780
-
780
+
781
781
  return Datet.in(self[col_name]).out(*method_args)
782
782
  end
783
783
  end
784
-
784
+
785
785
  if !inst_methods.include?(col_name)
786
786
  define_method(col_name) do |*method_args|
787
787
  return false if Datet.is_nullstamp?(self[col_name])
@@ -789,7 +789,7 @@ class Baza::Model
789
789
  end
790
790
  end
791
791
  end
792
-
792
+
793
793
  #Define various methods based on integer-columns.
794
794
  def self.define_numeric_methods(inst_methods, col_name)
795
795
  if !inst_methods.include?("#{col_name}_format".to_sym)
@@ -798,7 +798,7 @@ class Baza::Model
798
798
  end
799
799
  end
800
800
  end
801
-
801
+
802
802
  #Define methods to look up objects directly.
803
803
  #===Examples
804
804
  # user = Models::User.by_username('John Doe')
@@ -810,7 +810,7 @@ class Baza::Model
810
810
  end
811
811
  end
812
812
  end
813
-
813
+
814
814
  #Defines dbtime-methods based on time-columns.
815
815
  def self.define_time_methods(inst_methods, col_name)
816
816
  if !inst_methods.include?("#{col_name}_dbt".to_sym)
@@ -819,52 +819,52 @@ class Baza::Model
819
819
  end
820
820
  end
821
821
  end
822
-
822
+
823
823
  #Memory friendly helper method that defines methods for 'has_many'.
824
824
  def self.define_many_methods(classname, methodname, colname, where_args)
825
825
  define_method(methodname) do |*args, &block|
826
826
  if args and args[0]
827
- list_args = args[0]
827
+ list_args = args[0]
828
828
  else
829
829
  list_args = {}
830
830
  end
831
-
831
+
832
832
  list_args.merge!(where_args) if where_args
833
833
  list_args[colname.to_s] = self.id
834
-
834
+
835
835
  return self.class.ob.list(classname, list_args, &block)
836
836
  end
837
-
837
+
838
838
  define_method("#{methodname}_count".to_sym) do |*args|
839
839
  list_args = args[0] if args and args[0]
840
840
  list_args = {} if !list_args
841
841
  list_args[colname.to_s] = self.id
842
842
  list_args["count"] = true
843
-
843
+
844
844
  return self.class.ob.list(classname, list_args)
845
845
  end
846
-
846
+
847
847
  define_method("#{methodname}_last".to_sym) do |args|
848
848
  args = {} if !args
849
849
  return self.class.ob.list(classname, {"orderby" => [["id", "desc"]], "limit" => 1}.merge(args))
850
850
  end
851
851
  end
852
-
852
+
853
853
  #Memory friendly helper method that defines methods for 'has_one'.
854
854
  def self.define_one_methods(classname, methodname, colname)
855
855
  define_method(methodname) do
856
856
  return self.class.ob.get_try(self, colname, classname)
857
857
  end
858
-
858
+
859
859
  methodname_html = "#{methodname}_html".to_sym
860
860
  define_method(methodname_html) do |*args|
861
861
  obj = self.__send__(methodname)
862
862
  return self.class.ob.events.call(:no_html, classname) if !obj
863
-
863
+
864
864
  raise "Class '#{classname}' does not have a 'html'-method." if !obj.respond_to?(:html)
865
865
  return obj.html(*args)
866
866
  end
867
-
867
+
868
868
  methodname_name = "#{methodname}_name".to_sym
869
869
  define_method(methodname_name) do |*args|
870
870
  obj = self.__send__(methodname)
@@ -872,14 +872,14 @@ class Baza::Model
872
872
  return obj.name(*args)
873
873
  end
874
874
  end
875
-
875
+
876
876
  #Returns a hash reflection the current ActiveRecord model and its current values (not like .attributes which reflects the old values).
877
877
  def self.activerecord_to_hash(model)
878
878
  attrs = {}
879
879
  model.attribute_names.each do |name|
880
880
  attrs[name] = model.__send__(name)
881
881
  end
882
-
882
+
883
883
  return attrs
884
884
  end
885
885
  end