baza 0.0.19 → 0.0.20

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +157 -0
  3. data/Gemfile +11 -9
  4. data/Gemfile.lock +41 -9
  5. data/README.md +1 -1
  6. data/Rakefile +19 -16
  7. data/VERSION +1 -1
  8. data/baza.gemspec +31 -7
  9. data/config/best_project_practice_rubocop.yml +2 -0
  10. data/config/best_project_practice_rubocop_todo.yml +157 -0
  11. data/lib/baza/base_sql_driver.rb +79 -20
  12. data/lib/baza/cloner.rb +2 -2
  13. data/lib/baza/column.rb +6 -0
  14. data/lib/baza/database.rb +33 -0
  15. data/lib/baza/database_model.rb +26 -0
  16. data/lib/baza/database_model_functionality.rb +10 -0
  17. data/lib/baza/database_model_name.rb +16 -0
  18. data/lib/baza/db.rb +210 -413
  19. data/lib/baza/dbtime.rb +20 -20
  20. data/lib/baza/driver.rb +3 -3
  21. data/lib/baza/drivers/active_record/columns.rb +1 -1
  22. data/lib/baza/drivers/active_record/indexes.rb +1 -1
  23. data/lib/baza/drivers/active_record/result.rb +3 -5
  24. data/lib/baza/drivers/active_record/tables.rb +2 -2
  25. data/lib/baza/drivers/active_record.rb +30 -16
  26. data/lib/baza/drivers/mysql/column.rb +51 -48
  27. data/lib/baza/drivers/mysql/columns.rb +8 -8
  28. data/lib/baza/drivers/mysql/database.rb +28 -0
  29. data/lib/baza/drivers/mysql/databases.rb +35 -0
  30. data/lib/baza/drivers/mysql/index.rb +32 -22
  31. data/lib/baza/drivers/mysql/result.rb +24 -24
  32. data/lib/baza/drivers/mysql/sqlspecs.rb +1 -1
  33. data/lib/baza/drivers/mysql/table.rb +80 -91
  34. data/lib/baza/drivers/mysql/tables.rb +30 -32
  35. data/lib/baza/drivers/mysql/unbuffered_result.rb +5 -5
  36. data/lib/baza/drivers/mysql.rb +127 -220
  37. data/lib/baza/drivers/mysql2/database.rb +2 -0
  38. data/lib/baza/drivers/mysql2/databases.rb +2 -0
  39. data/lib/baza/drivers/mysql2/result.rb +3 -5
  40. data/lib/baza/drivers/mysql2.rb +21 -106
  41. data/lib/baza/drivers/mysql_java/database.rb +2 -0
  42. data/lib/baza/drivers/mysql_java/databases.rb +2 -0
  43. data/lib/baza/drivers/mysql_java.rb +16 -12
  44. data/lib/baza/drivers/sqlite3/column.rb +33 -33
  45. data/lib/baza/drivers/sqlite3/columns.rb +6 -6
  46. data/lib/baza/drivers/sqlite3/database.rb +2 -0
  47. data/lib/baza/drivers/sqlite3/databases.rb +25 -0
  48. data/lib/baza/drivers/sqlite3/index.rb +20 -6
  49. data/lib/baza/drivers/sqlite3/result.rb +16 -17
  50. data/lib/baza/drivers/sqlite3/sqlspecs.rb +1 -1
  51. data/lib/baza/drivers/sqlite3/table.rb +99 -100
  52. data/lib/baza/drivers/sqlite3/tables.rb +30 -23
  53. data/lib/baza/drivers/sqlite3/unbuffered_result.rb +2 -2
  54. data/lib/baza/drivers/sqlite3.rb +27 -14
  55. data/lib/baza/drivers/sqlite3_java/database.rb +2 -0
  56. data/lib/baza/drivers/sqlite3_java/unbuffered_result.rb +2 -2
  57. data/lib/baza/drivers/sqlite3_java.rb +12 -10
  58. data/lib/baza/drivers/sqlite3_rhodes.rb +9 -7
  59. data/lib/baza/dump.rb +55 -55
  60. data/lib/baza/errors.rb +5 -0
  61. data/lib/baza/idquery.rb +25 -25
  62. data/lib/baza/index.rb +6 -0
  63. data/lib/baza/jdbc_driver.rb +6 -8
  64. data/lib/baza/jdbc_result.rb +1 -1
  65. data/lib/baza/model.rb +299 -309
  66. data/lib/baza/model_custom.rb +74 -72
  67. data/lib/baza/model_handler.rb +244 -255
  68. data/lib/baza/model_handler_sqlhelper.rb +122 -132
  69. data/lib/baza/mysql_base_driver.rb +112 -0
  70. data/lib/baza/query_buffer.rb +23 -23
  71. data/lib/baza/result_base.rb +2 -2
  72. data/lib/baza/revision.rb +67 -67
  73. data/lib/baza/row.rb +82 -84
  74. data/lib/baza/sqlspecs.rb +1 -1
  75. data/lib/baza/table.rb +31 -0
  76. data/lib/baza.rb +43 -10
  77. data/shippable.yml +1 -0
  78. data/spec/cloner_spec.rb +4 -4
  79. data/spec/drivers/active_record_mysql2_spec.rb +3 -3
  80. data/spec/drivers/active_record_mysql_spec.rb +2 -2
  81. data/spec/drivers/active_record_sqlite3_spec.rb +2 -2
  82. data/spec/drivers/mysql2_spec.rb +1 -0
  83. data/spec/drivers/mysql_spec.rb +1 -0
  84. data/spec/drivers/sqlite3_spec.rb +4 -5
  85. data/spec/info_active_record_example.rb +4 -6
  86. data/spec/info_active_record_mysql.rb +6 -7
  87. data/spec/info_active_record_mysql2.rb +2 -4
  88. data/spec/info_active_record_mysql2_shippable.rb +2 -4
  89. data/spec/info_active_record_mysql_shippable.rb +2 -4
  90. data/spec/info_active_record_sqlite3.rb +5 -6
  91. data/spec/info_mysql2_example.rb +1 -1
  92. data/spec/info_mysql2_shippable.rb +1 -1
  93. data/spec/info_mysql_example.rb +1 -1
  94. data/spec/info_mysql_shippable.rb +1 -1
  95. data/spec/info_sqlite3.rb +4 -3
  96. data/spec/model_handler_spec.rb +102 -115
  97. data/spec/spec_helper.rb +5 -5
  98. data/spec/support/driver_collection.rb +98 -95
  99. data/spec/support/driver_columns_collection.rb +18 -22
  100. data/spec/support/driver_databases_collection.rb +29 -0
  101. data/spec/support/driver_indexes_collection.rb +7 -6
  102. data/spec/support/driver_tables_collection.rb +38 -26
  103. metadata +49 -3
@@ -5,45 +5,45 @@ class Baza::ModelHandler
5
5
  attr_reader :args, :events, :data, :ids_cache, :ids_cache_should
6
6
 
7
7
  def initialize(args)
8
- require 'array_enumerator' if args[:array_enum]
9
- require 'event_handler'
10
- require 'monitor'
11
- require 'ostruct'
8
+ require "array_enumerator" if args[:array_enum]
9
+ require "event_handler"
10
+ require "monitor"
11
+ require "ostruct"
12
12
 
13
13
  @callbacks = {}
14
14
  @args = args
15
- @args[:col_id] = :id if !@args[:col_id]
16
- @args[:class_pre] = "class_" if !@args[:class_pre]
17
- @args[:module] = Kernel if !@args[:module]
18
- @args[:cache] = :weak if !@args.key?(:cache)
15
+ @args[:col_id] = :id unless @args[:col_id]
16
+ @args[:class_pre] = "class_" unless @args[:class_pre]
17
+ @args[:module] = Kernel unless @args[:module]
18
+ @args[:cache] = :weak unless @args.key?(:cache)
19
19
  @objects = {}
20
20
  @locks = {}
21
21
  @data = {}
22
22
  @lock_require = Monitor.new
23
23
 
24
- #Set up various events.
24
+ # Set up various events.
25
25
  @events = EventHandler.new
26
- @events.add_event(:name => :no_html, :connections_max => 1)
27
- @events.add_event(:name => :no_name, :connections_max => 1)
28
- @events.add_event(:name => :no_date, :connections_max => 1)
29
- @events.add_event(:name => :missing_class, :connections_max => 1)
30
- @events.add_event(:name => :require_class, :connections_max => 1)
26
+ @events.add_event(name: :no_html, connections_max: 1)
27
+ @events.add_event(name: :no_name, connections_max: 1)
28
+ @events.add_event(name: :no_date, connections_max: 1)
29
+ @events.add_event(name: :missing_class, connections_max: 1)
30
+ @events.add_event(name: :require_class, connections_max: 1)
31
31
 
32
- raise "No DB given." if !@args[:db] and !@args[:custom]
33
- raise "No class path given." if !@args[:class_path] and (@args[:require] or !@args.key?(:require))
32
+ raise "No DB given." if !@args[:db] && !@args[:custom]
33
+ raise "No class path given." if !@args[:class_path] && (@args[:require] || !@args.key?(:require))
34
34
 
35
35
  if args[:require_all]
36
36
  loads = []
37
37
 
38
38
  Dir.foreach(@args[:class_path]) do |file|
39
- next if file == "." or file == ".." or !file.match(/\.rb$/)
39
+ next if file == "." || file == ".." || !file.match(/\.rb$/)
40
40
  file_parsed = file
41
41
  file_parsed.gsub!(@args[:class_pre], "") if @args.key?(:class_pre)
42
42
  file_parsed.gsub!(/\.rb$/, "")
43
43
  file_parsed = StringCases.snake_to_camel(file_parsed)
44
44
 
45
45
  loads << file_parsed
46
- self.requireclass(file_parsed, {:load => false})
46
+ requireclass(file_parsed, load: false)
47
47
  end
48
48
 
49
49
  loads.each do |load_class|
@@ -51,20 +51,20 @@ class Baza::ModelHandler
51
51
  end
52
52
  end
53
53
 
54
- #Set up ID-caching.
54
+ # Set up ID-caching.
55
55
  @ids_cache_should = {}
56
56
 
57
- if @args[:models]
58
- @ids_cache = {}
57
+ return unless @args[:models]
59
58
 
60
- @args[:models].each do |classname, classargs|
61
- @ids_cache_should[classname] = true if classargs[:cache_ids]
62
- self.cache_ids(classname)
63
- end
59
+ @ids_cache = {}
60
+
61
+ @args[:models].each do |classname, classargs|
62
+ @ids_cache_should[classname] = true if classargs[:cache_ids]
63
+ cache_ids(classname)
64
64
  end
65
65
  end
66
66
 
67
- #Caches all IDs for a specific classname.
67
+ # Caches all IDs for a specific classname.
68
68
  def cache_ids(classname)
69
69
  classname = classname.to_sym
70
70
  return nil if !@ids_cache_should || !@ids_cache_should[classname]
@@ -95,7 +95,7 @@ class Baza::ModelHandler
95
95
  @locks.delete(classname)
96
96
  end
97
97
 
98
- #Returns a cloned version of the @objects variable. Cloned because iteration on it may crash some of the other methods in Ruby 1.9+
98
+ # Returns a cloned version of the @objects variable. Cloned because iteration on it may crash some of the other methods in Ruby 1.9+
99
99
  def objects
100
100
  objs_cloned = {}
101
101
 
@@ -103,58 +103,58 @@ class Baza::ModelHandler
103
103
  objs_cloned[key] = @objects[key].clone
104
104
  end
105
105
 
106
- return objs_cloned
106
+ objs_cloned
107
107
  end
108
108
 
109
- #Returns the database-connection used by this instance of Objects.
109
+ # Returns the database-connection used by this instance of Objects.
110
110
  def db
111
- return @args[:db]
111
+ @args[:db]
112
112
  end
113
113
 
114
- #Returns the total count of objects currently held by this instance.
114
+ # Returns the total count of objects currently held by this instance.
115
115
  def count_objects
116
116
  count = 0
117
117
  @objects.keys.each do |key|
118
118
  count += @objects[key].length
119
119
  end
120
120
 
121
- return count
121
+ count
122
122
  end
123
123
 
124
- #This connects a block to an event. When the event is called the block will be executed.
124
+ # This connects a block to an event. When the event is called the block will be executed.
125
125
  def connect(args, &block)
126
- raise "No object given." if !args["object"]
127
- raise "No signals given." if !args.key?("signal") and !args.key?("signals")
126
+ raise "No object given." unless args["object"]
127
+ raise "No signals given." if !args.key?("signal") && !args.key?("signals")
128
128
  args["block"] = block if block_given?
129
129
  object = args["object"].to_sym
130
130
 
131
- @callbacks[object] = {} if !@callbacks[object]
131
+ @callbacks[object] = {} unless @callbacks[object]
132
132
  conn_id = @callbacks[object].length.to_s
133
133
  @callbacks[object][conn_id] = args
134
- return conn_id
134
+ conn_id
135
135
  end
136
136
 
137
- #Returns true if the given signal is connected to the given object.
137
+ # Returns true if the given signal is connected to the given object.
138
138
  def connected?(args)
139
- raise "No object given." if !args["object"]
140
- raise "No signal given." if !args.key?("signal")
139
+ raise "No object given." unless args["object"]
140
+ raise "No signal given." unless args.key?("signal")
141
141
  object = args["object"].to_sym
142
142
 
143
143
  if @callbacks.key?(object)
144
- @callbacks[object].clone.each do |ckey, callback|
145
- return true if callback.key?("signal") and callback["signal"].to_s == args["signal"].to_s
146
- return true if callback.key?("signals") and (callback["signals"].include?(args["signal"].to_s) or callback["signals"].include?(args["signal"].to_sym))
144
+ @callbacks[object].clone.each do |_ckey, callback|
145
+ return true if callback.key?("signal") && callback["signal"].to_s == args["signal"].to_s
146
+ return true if callback.key?("signals") && (callback["signals"].include?(args["signal"].to_s) || callback["signals"].include?(args["signal"].to_sym))
147
147
  end
148
148
  end
149
149
 
150
- return false
150
+ false
151
151
  end
152
152
 
153
- #Unconnects a connect by 'object' and 'conn_id'.
153
+ # Unconnects a connect by 'object' and 'conn_id'.
154
154
  def unconnect(args)
155
- raise ArgumentError, "No object given." if !args["object"]
155
+ raise ArgumentError, "No object given." unless args["object"]
156
156
  object = args["object"].to_sym
157
- raise ArgumentError, "Object doesnt exist: '#{object}'." if !@callbacks.key?(object)
157
+ raise ArgumentError, "Object doesnt exist: '#{object}'." unless @callbacks.key?(object)
158
158
 
159
159
  if args["conn_id"]
160
160
  conn_ids = [args["conn_id"]]
@@ -165,42 +165,42 @@ class Baza::ModelHandler
165
165
  end
166
166
 
167
167
  conn_ids.each do |conn_id|
168
- raise Errno::ENOENT, "Conn ID doest exist: '#{conn_id}' (#{args})." if !@callbacks[object].key?(conn_id)
168
+ raise Errno::ENOENT, "Conn ID doest exist: '#{conn_id}' (#{args})." unless @callbacks[object].key?(conn_id)
169
169
  @callbacks[object].delete(conn_id)
170
170
  end
171
171
  end
172
172
 
173
- #This method is used to call the connected callbacks for an event.
174
- def call(args, &block)
173
+ # This method is used to call the connected callbacks for an event.
174
+ def call(args, &_block)
175
175
  classstr = args["object"].class.classname.to_sym
176
176
 
177
- if @callbacks.key?(classstr)
178
- @callbacks[classstr].clone.each do |callback_key, callback|
179
- docall = false
177
+ return unless @callbacks.key?(classstr)
180
178
 
181
- if callback.key?("signal") and args.key?("signal") and callback["signal"].to_s == args["signal"].to_s
182
- docall = true
183
- elsif callback["signals"] and args["signal"] and (callback["signals"].include?(args["signal"].to_s) or callback["signals"].include?(args["signal"].to_sym))
184
- docall = true
185
- end
179
+ @callbacks[classstr].clone.each do |_callback_key, callback|
180
+ docall = false
186
181
 
187
- next if !docall
182
+ if callback.key?("signal") && args.key?("signal") && callback["signal"].to_s == args["signal"].to_s
183
+ docall = true
184
+ elsif callback["signals"] && args["signal"] && (callback["signals"].include?(args["signal"].to_s) || callback["signals"].include?(args["signal"].to_sym))
185
+ docall = true
186
+ end
188
187
 
189
- if callback["block"]
190
- callargs = []
191
- arity = callback["block"].arity
192
- if arity <= 0
193
- #do nothing
194
- elsif arity == 1
195
- callargs << args["object"]
196
- else
197
- raise "Unknown number of arguments: #{arity}"
198
- end
188
+ next unless docall
199
189
 
200
- callback["block"].call(*callargs)
190
+ if callback["block"]
191
+ callargs = []
192
+ arity = callback["block"].arity
193
+ if arity <= 0
194
+ # do nothing
195
+ elsif arity == 1
196
+ callargs << args["object"]
201
197
  else
202
- raise "No valid callback given."
198
+ raise "Unknown number of arguments: #{arity}"
203
199
  end
200
+
201
+ callback["block"].call(*callargs)
202
+ else
203
+ raise "No valid callback given."
204
204
  end
205
205
  end
206
206
  end
@@ -211,28 +211,26 @@ class Baza::ModelHandler
211
211
  classname_snake = StringCases.camel_to_snake(classname)
212
212
 
213
213
  @lock_require.synchronize do
214
- #Maybe the classname got required meanwhile the synchronized wait - check again.
214
+ # Maybe the classname got required meanwhile the synchronized wait - check again.
215
215
  return false if @objects.key?(classname)
216
216
 
217
217
  if @events.connected?(:require_class)
218
- @events.call(:require_class, {
219
- :class => classname
220
- })
218
+ @events.call(:require_class, class: classname)
221
219
  else
222
220
  doreq = false
223
221
 
224
222
  if args[:require]
225
223
  doreq = true
226
- elsif args.key?(:require) and !args[:require]
224
+ elsif args.key?(:require) && !args[:require]
227
225
  doreq = false
228
- elsif @args[:require] or !@args.key?(:require)
226
+ elsif @args[:require] || !@args.key?(:require)
229
227
  doreq = true
230
228
  end
231
229
 
232
230
  if doreq
233
231
  filename = "#{@args[:class_path]}/#{@args[:class_pre]}#{classname_snake}.rb"
234
232
  filename_req = "#{@args[:class_path]}/#{@args[:class_pre]}#{classname_snake}"
235
- raise "Class file could not be found: #{filename}." if !File.exists?(filename)
233
+ raise "Class file could not be found: #{filename}." unless File.exist?(filename)
236
234
  require filename_req
237
235
  end
238
236
  end
@@ -244,9 +242,7 @@ class Baza::ModelHandler
244
242
  classob = @args[:module].const_get(classname)
245
243
  rescue NameError => e
246
244
  if @events.connected?(:missing_class)
247
- @events.call(:missing_class, {
248
- :class => classname
249
- })
245
+ @events.call(:missing_class, class: classname)
250
246
  classob = @args[:module].const_get(classname)
251
247
  else
252
248
  raise e
@@ -254,15 +250,15 @@ class Baza::ModelHandler
254
250
  end
255
251
  end
256
252
 
257
- if (classob.respond_to?(:load_columns) or classob.respond_to?(:datarow_init)) and (!args.key?(:load) or args[:load])
258
- self.load_class(classname, args)
253
+ if (classob.respond_to?(:load_columns) || classob.respond_to?(:datarow_init)) && (!args.key?(:load) || args[:load])
254
+ load_class(classname, args)
259
255
  end
260
256
 
261
- self.init_class(classname)
257
+ init_class(classname)
262
258
  end
263
259
  end
264
260
 
265
- #Loads a Datarow-class by calling various static methods.
261
+ # Loads a Datarow-class by calling various static methods.
266
262
  def load_class(classname, args = {})
267
263
  if args[:class]
268
264
  classob = args[:class]
@@ -270,32 +266,32 @@ class Baza::ModelHandler
270
266
  classob = @args[:module].const_get(classname)
271
267
  end
272
268
 
273
- pass_arg = OpenStruct.new(:ob => self, :db => @args[:db])
269
+ pass_arg = OpenStruct.new(ob: self, db: @args[:db])
274
270
  classob.load_columns(pass_arg) if classob.respond_to?(:load_columns)
275
271
  classob.datarow_init(pass_arg) if classob.respond_to?(:datarow_init)
276
272
  end
277
273
 
278
- #Returns the instance of classname, but only if it already exists.
274
+ # Returns the instance of classname, but only if it already exists.
279
275
  def get_if_cached(classname, id)
280
276
  classname = classname.to_sym
281
277
  id = id.to_i
282
278
 
283
- if wref_map = @objects[classname] and obj = wref_map.get(id)
279
+ if wref_map = @objects[classname] && obj = wref_map.get(id)
284
280
  return obj
285
281
  end
286
282
 
287
- return nil
283
+ nil
288
284
  end
289
285
 
290
- #Returns true if a row of the given classname and the ID exists. Will use ID-cache if set in arguments and spawned otherwise it will do an actual lookup.
286
+ # Returns true if a row of the given classname and the ID exists. Will use ID-cache if set in arguments and spawned otherwise it will do an actual lookup.
291
287
  #===Examples
292
288
  # print "User 5 exists." if ob.exists?(:User, 5)
293
289
  def exists?(classname, id)
294
- #Make sure the given data are in the correct types.
290
+ # Make sure the given data are in the correct types.
295
291
  classname = classname.to_sym
296
292
  id = id.to_i
297
293
 
298
- #Check if ID-cache is enabled for that classname. Avoid SQL-lookup by using that.
294
+ # Check if ID-cache is enabled for that classname. Avoid SQL-lookup by using that.
299
295
  if @ids_cache_should.key?(classname)
300
296
  if @ids_cache[classname].key?(id)
301
297
  return true
@@ -304,13 +300,13 @@ class Baza::ModelHandler
304
300
  end
305
301
  end
306
302
 
307
- #If the object currently exists in cache, we dont have to do a lookup either.
308
- return true if @objects.key?(classname) and obj = @objects[classname].get(id) and !obj.deleted?
303
+ # If the object currently exists in cache, we dont have to do a lookup either.
304
+ return true if @objects.key?(classname) && (obj = @objects[classname].get(id)) && !obj.deleted?
309
305
 
310
- #Okay - no other options than to actually do a real lookup.
306
+ # Okay - no other options than to actually do a real lookup.
311
307
  begin
312
308
  table = @args[:module].const_get(classname).table
313
- row = @args[:db].single(table, {@args[:col_id] => id})
309
+ row = @args[:db].single(table, @args[:col_id] => id)
314
310
 
315
311
  if row
316
312
  return true
@@ -322,99 +318,96 @@ class Baza::ModelHandler
322
318
  end
323
319
  end
324
320
 
325
- #Gets an object from the ID or the full data-hash in the database.
321
+ # Gets an object from the ID or the full data-hash in the database.
326
322
  #===Examples
327
323
  # inst = ob.get(:User, 5)
328
324
  def get(classname, data, args = nil)
329
325
  classname = classname.to_sym
330
326
 
331
- if data.is_a?(Integer) or data.is_a?(String) or data.is_a?(Fixnum)
327
+ if data.is_a?(Integer) || data.is_a?(String) || data.is_a?(Fixnum)
332
328
  id = data.to_i
333
- elsif data.is_a?(Hash) and data.key?(@args[:col_id].to_sym)
329
+ elsif data.is_a?(Hash) && data.key?(@args[:col_id].to_sym)
334
330
  id = data[@args[:col_id].to_sym].to_i
335
- elsif data.is_a?(Hash) and data.key?(@args[:col_id].to_s)
331
+ elsif data.is_a?(Hash) && data.key?(@args[:col_id].to_s)
336
332
  id = data[@args[:col_id].to_s].to_i
337
- elsif
338
- raise ArgumentError, "Unknown data for class '#{classname}': '#{data.class.to_s}' (#{data})."
333
+ else
334
+ raise ArgumentError, "Unknown data for class '#{classname}': '#{data.class}' (#{data})."
339
335
  end
340
336
 
341
337
  if @objects.key?(classname)
342
338
  case @args[:cache]
343
- when :weak
344
- if obj = @objects[classname].get(id) and obj.id.to_i == id
345
- return obj
346
- end
347
- else
348
- return @objects[classname][id] if @objects[classname].key?(id)
339
+ when :weak
340
+ if (obj = @objects[classname].get(id)) && obj.id.to_i == id
341
+ return obj
342
+ end
343
+ else
344
+ return @objects[classname][id] if @objects[classname].key?(id)
349
345
  end
350
346
  end
351
347
 
352
- self.requireclass(classname) if !@objects.key?(classname)
348
+ requireclass(classname) unless @objects.key?(classname)
353
349
 
354
350
  @locks[classname].synchronize do
355
- #Maybe the object got spawned while we waited for the lock? If so we shouldnt spawn another instance.
356
- if @args[:cache] == :weak and obj = @objects[classname].get(id) and obj.id.to_i == id
351
+ # Maybe the object got spawned while we waited for the lock? If so we shouldnt spawn another instance.
352
+ if @args[:cache] == :weak && obj = @objects[classname].get(id) && obj.id.to_i == id
357
353
  return obj
358
354
  end
359
355
 
360
- #Spawn object.
356
+ # Spawn object.
361
357
  obj = @args[:module].const_get(classname).new(data, args)
362
358
 
363
- #Save object in cache.
359
+ # Save object in cache.
364
360
  case @args[:cache]
365
- when :none
366
- return obj
367
- else
368
- @objects[classname][id] = obj
369
- return obj
361
+ when :none
362
+ return obj
363
+ else
364
+ @objects[classname][id] = obj
365
+ return obj
370
366
  end
371
367
  end
372
368
 
373
369
  raise "Unexpected run?"
374
370
  end
375
371
 
376
- #Same as normal get but returns false if not found instead of raising error.
372
+ # Same as normal get but returns false if not found instead of raising error.
377
373
  def get!(*args, &block)
378
- begin
379
- return self.get(*args, &block)
380
- rescue Errno::ENOENT
381
- return false
382
- end
374
+ return get(*args, &block)
375
+ rescue Errno::ENOENT
376
+ return false
383
377
  end
384
378
 
385
379
  def object_finalizer(id)
386
380
  classname = @objects_idclass[id]
387
- if classname
388
- @objects[classname].delete(id)
389
- @objects_idclass.delete(id)
390
- end
381
+ return unless classname
382
+ @objects[classname].delete(id)
383
+ @objects_idclass.delete(id)
391
384
  end
392
385
 
393
- #Returns the first object found from the given arguments. Also automatically limits the results to 1.
386
+ # Returns the first object found from the given arguments. Also automatically limits the results to 1.
394
387
  def get_by(classname, args = {})
395
388
  classname = classname.to_sym
396
- self.requireclass(classname)
389
+ requireclass(classname)
397
390
  classob = @args[:module].const_get(classname)
398
391
 
399
- raise "list-function has not been implemented for '#{classname}'." if !classob.respond_to?(:list)
392
+ raise "list-function has not been implemented for '#{classname}'." unless classob.respond_to?(:list)
400
393
 
401
394
  args["limit"] = 1
402
- self.list(classname, args) do |obj|
395
+ list(classname, args) do |obj|
403
396
  return obj
404
397
  end
405
398
 
406
- return false
399
+ false
407
400
  end
408
401
 
409
- #Searches for an object with the given data. If not found it creates it. Returns the found or created object in the end.
410
- def get_or_add(classname, data, args = nil)
411
- obj = self.get_by(classname, data.clone)
412
- obj = self.add(classname, data) if !obj
413
- return obj
402
+ # Searches for an object with the given data. If not found it creates it. Returns the found or created object in the end.
403
+ def get_or_add(classname, data, _args = nil)
404
+ obj = get_by(classname, data.clone)
405
+ obj = add(classname, data) unless obj
406
+ obj
414
407
  end
415
408
 
416
409
  def get_try(obj, col_name, obj_name = nil)
417
- if !obj_name
410
+ unless obj_name
418
411
  if match = col_name.to_s.match(/^(.+)_id$/)
419
412
  obj_name = Php4r.ucwords(match[1]).to_sym
420
413
  else
@@ -426,13 +419,13 @@ class Baza::ModelHandler
426
419
  return false if id_data.to_i <= 0
427
420
 
428
421
  begin
429
- return self.get(obj_name, id_data)
422
+ return get(obj_name, id_data)
430
423
  rescue Errno::ENOENT
431
424
  return false
432
425
  end
433
426
  end
434
427
 
435
- #Returns an array-list of objects. If given a block the block will be called for each element and memory will be spared if running weak-link-mode.
428
+ # Returns an array-list of objects. If given a block the block will be called for each element and memory will be spared if running weak-link-mode.
436
429
  #===Examples
437
430
  # ob.list(:User) do |user|
438
431
  # print "Username: #{user.name}\n"
@@ -440,19 +433,19 @@ class Baza::ModelHandler
440
433
  def list(classname, args = {}, &block)
441
434
  args = {} if args == nil
442
435
  classname = classname.to_sym
443
- self.requireclass(classname)
436
+ requireclass(classname)
444
437
  classob = @args[:module].const_get(classname)
445
438
 
446
- raise "list-function has not been implemented for '#{classname}'." if !classob.respond_to?("list")
447
- ret = classob.list(OpenStruct.new(:args => args, :ob => self, :db => @args[:db]), &block)
439
+ raise "list-function has not been implemented for '#{classname}'." unless classob.respond_to?("list")
440
+ ret = classob.list(OpenStruct.new(args: args, ob: self, db: @args[:db]), &block)
448
441
 
449
- #If 'ret' is an array and a block is given then the list-method didnt return blocks. We emulate it instead with the following code.
450
- if block and ret.is_a?(Array)
442
+ # If 'ret' is an array and a block is given then the list-method didnt return blocks. We emulate it instead with the following code.
443
+ if block && ret.is_a?(Array)
451
444
  ret.each do |obj|
452
445
  block.call(obj)
453
446
  end
454
447
  return nil
455
- elsif block and ret != nil
448
+ elsif block && !ret.nil?
456
449
  raise "Return should return nil because of block but didnt. It wasnt an array either..."
457
450
  elsif block
458
451
  return nil
@@ -461,24 +454,24 @@ class Baza::ModelHandler
461
454
  end
462
455
  end
463
456
 
464
- #Yields every object that is missing certain required objects (based on 'has_many' required-argument).
457
+ # Yields every object that is missing certain required objects (based on 'has_many' required-argument).
465
458
  def list_invalid_required(args, &block)
466
459
  enum = Enumerator.new do |yielder|
467
460
  classname = args[:class]
468
461
  classob = @args[:module].const_get(classname)
469
462
  required_data = classob.required_data
470
463
 
471
- if required_data and !required_data.empty?
464
+ if required_data && !required_data.empty?
472
465
  required_data.each do |req_data|
473
- self.list(args[:class]) do |obj|
466
+ list(args[:class]) do |obj|
474
467
  puts "Checking #{obj.classname}(#{obj.id}) for required #{req_data[:class]}." if args[:debug]
475
468
  id = obj[req_data[:col]]
476
469
 
477
470
  begin
478
- raise Errno::ENOENT if !id
479
- obj_req = self.get(req_data[:class], id)
471
+ raise Errno::ENOENT unless id
472
+ obj_req = get(req_data[:class], id)
480
473
  rescue Errno::ENOENT
481
- yielder << {:obj => obj, :type => :required, :id => id, :data => req_data}
474
+ yielder << {obj: obj, type: :required, id: id, data: req_data}
482
475
  end
483
476
  end
484
477
  end
@@ -492,7 +485,7 @@ class Baza::ModelHandler
492
485
  end
493
486
  end
494
487
 
495
- #Returns select-options-HTML for inserting into a HTML-select-element.
488
+ # Returns select-options-HTML for inserting into a HTML-select-element.
496
489
  def list_opts(classname, args = {})
497
490
  Knj::ArrayExt.hash_sym(args)
498
491
  classname = classname.to_sym
@@ -505,23 +498,23 @@ class Baza::ModelHandler
505
498
 
506
499
  html = ""
507
500
 
508
- if args[:addnew] or args[:add]
501
+ if args[:addnew] || args[:add]
509
502
  html << "<option"
510
- html << " selected=\"selected\"" if !args[:selected]
503
+ html << " selected=\"selected\"" unless args[:selected]
511
504
  html << " value=\"\">#{_("Add new")}</option>"
512
505
  elsif args[:none]
513
506
  html << "<option"
514
- html << " selected=\"selected\"" if !args[:selected]
507
+ html << " selected=\"selected\"" unless args[:selected]
515
508
  html << " value=\"\">#{_("None")}</option>"
516
509
  end
517
510
 
518
- self.list(classname, args[:list_args]) do |object|
511
+ list(classname, args[:list_args]) do |object|
519
512
  html << "<option value=\"#{object.id.html}\""
520
513
 
521
514
  selected = false
522
- if args[:selected].is_a?(Array) and args[:selected].index(object) != nil
515
+ if args[:selected].is_a?(Array) && !args[:selected].index(object).nil?
523
516
  selected = true
524
- elsif args[:selected] and args[:selected].respond_to?("is_knj?") and args[:selected].id.to_s == object.id.to_s
517
+ elsif args[:selected] && args[:selected].respond_to?("is_knj?") && args[:selected].id.to_s == object.id.to_s
525
518
  selected = true
526
519
  end
527
520
 
@@ -530,9 +523,9 @@ class Baza::ModelHandler
530
523
  obj_methods = object.class.instance_methods(false)
531
524
 
532
525
  begin
533
- if obj_methods.index("name") != nil or obj_methods.index(:name) != nil
526
+ if !obj_methods.index("name").nil? || !obj_methods.index(:name).nil?
534
527
  objhtml = object.name.html
535
- elsif obj_methods.index("title") != nil or obj_methods.index(:title) != nil
528
+ elsif !obj_methods.index("title").nil? || !obj_methods.index(:title).nil?
536
529
  objhtml = object.title.html
537
530
  elsif object.respond_to?(:data)
538
531
  obj_data = object.data
@@ -546,17 +539,17 @@ class Baza::ModelHandler
546
539
  objhtml = ""
547
540
  end
548
541
 
549
- raise "Could not figure out which name-method to call?" if !objhtml
542
+ raise "Could not figure out which name-method to call?" unless objhtml
550
543
  html << ">#{objhtml}</option>"
551
544
  rescue => e
552
545
  html << ">[#{object.class.name}: #{e.message}]</option>"
553
546
  end
554
547
  end
555
548
 
556
- return html
549
+ html
557
550
  end
558
551
 
559
- #Returns a hash which can be used to generate HTML-select-elements.
552
+ # Returns a hash which can be used to generate HTML-select-elements.
560
553
  def list_optshash(classname, args = {})
561
554
  classname = classname.to_sym
562
555
 
@@ -568,7 +561,7 @@ class Baza::ModelHandler
568
561
 
569
562
  list = {}
570
563
 
571
- if args[:addnew] or args[:add]
564
+ if args[:addnew] || args[:add]
572
565
  list["0"] = _("Add new")
573
566
  elsif args[:choose]
574
567
  list["0"] = _("Choose") + ":"
@@ -588,22 +581,22 @@ class Baza::ModelHandler
588
581
  end
589
582
  end
590
583
 
591
- return list
584
+ list
592
585
  end
593
586
 
594
- #Returns a list of a specific object by running specific SQL against the database.
587
+ # Returns a list of a specific object by running specific SQL against the database.
595
588
  def list_bysql(classname, sql, args = nil, &block)
596
589
  classname = classname.to_sym
597
- ret = [] if !block
590
+ ret = [] unless block
598
591
  qargs = nil
599
592
 
600
593
  if args
601
- args.each do |key, val|
594
+ args.each do |key, _val|
602
595
  case key
603
- when :cloned_ubuf
604
- qargs = {:cloned_ubuf => true}
605
- else
606
- raise "Invalid key: '#{key}'."
596
+ when :cloned_ubuf
597
+ qargs = {cloned_ubuf: true}
598
+ else
599
+ raise "Invalid key: '#{key}'."
607
600
  end
608
601
  end
609
602
  end
@@ -611,7 +604,7 @@ class Baza::ModelHandler
611
604
  if @args[:array_enum]
612
605
  enum = Enumerator.new do |yielder|
613
606
  @args[:db].q(sql, qargs) do |d_obs|
614
- yielder << self.get(classname, d_obs)
607
+ yielder << get(classname, d_obs)
615
608
  end
616
609
  end
617
610
 
@@ -624,9 +617,9 @@ class Baza::ModelHandler
624
617
  else
625
618
  @args[:db].q(sql, qargs) do |d_obs|
626
619
  if block
627
- block.call(self.get(classname, d_obs))
620
+ block.call(get(classname, d_obs))
628
621
  else
629
- ret << self.get(classname, d_obs)
622
+ ret << get(classname, d_obs)
630
623
  end
631
624
  end
632
625
 
@@ -638,89 +631,89 @@ class Baza::ModelHandler
638
631
  end
639
632
  end
640
633
 
641
- #Add a new object to the database and to the cache.
634
+ # Add a new object to the database and to the cache.
642
635
  #===Examples
643
636
  # obj = ob.add(:User, {:username => "User 1"})
644
637
  def add(classname, data = {}, args = nil)
645
- raise "data-variable was not a hash: '#{data.class.name}'." if !data.is_a?(Hash)
638
+ raise "data-variable was not a hash: '#{data.class.name}'." unless data.is_a?(Hash)
646
639
  classname = classname.to_sym
647
- self.requireclass(classname)
640
+ requireclass(classname)
648
641
 
649
642
  if @args[:custom]
650
643
  classobj = @args[:module].const_get(classname)
651
644
  retob = classobj.add(OpenStruct.new(
652
- :ob => self,
653
- :data => data
645
+ ob: self,
646
+ data: data
654
647
  ))
655
648
  else
656
649
  classobj = @args[:module].const_get(classname)
657
650
 
658
- #Run the class 'add'-method to check various data.
659
- classobj.add(OpenStruct.new(:ob => self, :db => @args[:db], :data => data)) if classobj.respond_to?(:add)
651
+ # Run the class 'add'-method to check various data.
652
+ classobj.add(OpenStruct.new(ob: self, db: @args[:db], data: data)) if classobj.respond_to?(:add)
660
653
 
661
- #Check if various required data is given. If not then raise an error telling about it.
654
+ # Check if various required data is given. If not then raise an error telling about it.
662
655
  required_data = classobj.required_data
663
656
  required_data.each do |req_data|
664
- raise "No '#{req_data[:class]}' given by the data '#{req_data[:col]}'." if !data.key?(req_data[:col])
665
- raise "The '#{req_data[:class]}' by ID '#{data[req_data[:col]]}' could not be found with the data '#{req_data[:col]}'." if !self.exists?(req_data[:class], data[req_data[:col]])
657
+ raise "No '#{req_data[:class]}' given by the data '#{req_data[:col]}'." unless data.key?(req_data[:col])
658
+ raise "The '#{req_data[:class]}' by ID '#{data[req_data[:col]]}' could not be found with the data '#{req_data[:col]}'." unless self.exists?(req_data[:class], data[req_data[:col]])
666
659
  end
667
660
 
668
- #If 'skip_ret' is given, then the ID wont be looked up and the object wont be spawned. Be aware the connected events wont be executed either. In return it will go a lot faster.
669
- if args and args[:skip_ret] and !@ids_cache_should.key?(classname)
661
+ # If 'skip_ret' is given, then the ID wont be looked up and the object wont be spawned. Be aware the connected events wont be executed either. In return it will go a lot faster.
662
+ if args && args[:skip_ret] && !@ids_cache_should.key?(classname)
670
663
  ins_args = nil
671
664
  else
672
- ins_args = {:return_id => true}
665
+ ins_args = {return_id: true}
673
666
  end
674
667
 
675
- #Insert and (maybe?) get ID.
668
+ # Insert and (maybe?) get ID.
676
669
  ins_id = @args[:db].insert(classobj.table, data, ins_args).to_i
677
670
 
678
- #Add ID to ID-cache if ID-cache is active for that classname.
679
- @ids_cache[classname][ins_id] = true if ins_id != 0 and @ids_cache_should.key?(classname)
671
+ # Add ID to ID-cache if ID-cache is active for that classname.
672
+ @ids_cache[classname][ins_id] = true if ins_id != 0 && @ids_cache_should.key?(classname)
680
673
 
681
- #Skip the rest if we are told not to return result.
682
- return nil if args and args[:skip_ret]
674
+ # Skip the rest if we are told not to return result.
675
+ return nil if args && args[:skip_ret]
683
676
 
684
- #Spawn the object.
685
- retob = self.get(classname, ins_id, {:skip_reload => true})
677
+ # Spawn the object.
678
+ retob = get(classname, ins_id, skip_reload: true)
686
679
  end
687
680
 
688
- self.call("object" => retob, "signal" => "add")
681
+ call("object" => retob, "signal" => "add")
689
682
  retob.send(:add_after, {}) if retob.respond_to?(:add_after)
690
683
 
691
- return retob
684
+ retob
692
685
  end
693
686
 
694
- #Adds several objects to the database at once. This is faster than adding every single object by itself, since this will do multi-inserts if supported by the database.
687
+ # Adds several objects to the database at once. This is faster than adding every single object by itself, since this will do multi-inserts if supported by the database.
695
688
  #===Examples
696
689
  # ob.adds(:User, [{:username => "User 1"}, {:username => "User 2"})
697
690
  def adds(classname, datas)
698
691
  if @args[:module].const_get(classname).respond_to?(:add)
699
692
  datas.each do |data|
700
693
  @args[:module].const_get(classname).add(OpenStruct.new(
701
- :ob => self,
702
- :db => self.db,
703
- :data => data
694
+ ob: self,
695
+ db: db,
696
+ data: data
704
697
  ))
705
698
  end
706
699
  end
707
700
 
708
701
  db.insert_multi(classname, datas)
709
- self.cache_ids(classname)
702
+ cache_ids(classname)
710
703
  end
711
704
 
712
- #Calls a static method on a class. Passes the d-variable which contains the Objects-object, database-reference and more...
705
+ # Calls a static method on a class. Passes the d-variable which contains the Objects-object, database-reference and more...
713
706
  def static(class_name, method_name, *args, &block)
714
707
  class_name = class_name
715
708
  method_name = method_name
716
709
 
717
- self.requireclass(class_name)
710
+ requireclass(class_name)
718
711
  class_obj = @args[:module].const_get(class_name)
719
712
 
720
- #Sometimes this raises the exception but actually responds to the class? Therefore commented out. - knj
721
- #raise "The class '#{class_obj.name}' has no such method: '#{method_name}' (#{class_obj.methods.sort.join(", ")})." if !class_obj.respond_to?(method_name)
713
+ # Sometimes this raises the exception but actually responds to the class? Therefore commented out. - knj
714
+ # raise "The class '#{class_obj.name}' has no such method: '#{method_name}' (#{class_obj.methods.sort.join(", ")})." if !class_obj.respond_to?(method_name)
722
715
 
723
- pass_args = [OpenStruct.new(:ob => self, :db => self.db)]
716
+ pass_args = [OpenStruct.new(ob: self, db: db)]
724
717
 
725
718
  args.each do |arg|
726
719
  pass_args << arg
@@ -729,7 +722,7 @@ class Baza::ModelHandler
729
722
  class_obj.send(method_name, *pass_args, &block)
730
723
  end
731
724
 
732
- #Unset object. Do this if you are sure, that there are no more references left. This will be done automatically when deleting it.
725
+ # Unset object. Do this if you are sure, that there are no more references left. This will be done automatically when deleting it.
733
726
  def unset(object)
734
727
  if object.is_a?(Array)
735
728
  object.each do |obj|
@@ -740,9 +733,7 @@ class Baza::ModelHandler
740
733
 
741
734
  classname = object.class.name
742
735
 
743
- if @args[:module]
744
- classname = classname.gsub(@args[:module].name + "::", "")
745
- end
736
+ classname = classname.gsub(@args[:module].name + "::", "") if @args[:module]
746
737
 
747
738
  classname = classname.to_sym
748
739
  @objects[classname].delete(object.id.to_i)
@@ -751,7 +742,7 @@ class Baza::ModelHandler
751
742
  def unset_class(classname)
752
743
  if classname.is_a?(Array)
753
744
  classname.each do |classn|
754
- self.unset_class(classn)
745
+ unset_class(classn)
755
746
  end
756
747
 
757
748
  return false
@@ -759,52 +750,52 @@ class Baza::ModelHandler
759
750
 
760
751
  classname = classname.to_sym
761
752
 
762
- return false if !@objects.key?(classname)
753
+ return false unless @objects.key?(classname)
763
754
  @objects.delete(classname)
764
755
  end
765
756
 
766
- #Delete an object. Both from the database and from the cache.
757
+ # Delete an object. Both from the database and from the cache.
767
758
  #===Examples
768
759
  # user = ob.get(:User, 1)
769
760
  # ob.delete(user)
770
761
  def delete(object, args = nil)
771
- #Return false if the object has already been deleted.
762
+ # Return false if the object has already been deleted.
772
763
  return false if object.deleted?
773
764
  classname = object.class.classname.to_sym
774
765
 
775
- self.call("object" => object, "signal" => "delete_before")
776
- self.unset(object)
766
+ call("object" => object, "signal" => "delete_before")
767
+ unset(object)
777
768
  obj_id = object.id
778
769
  object.delete if object.respond_to?(:delete)
779
770
 
780
- #If autodelete is set by 'has_many'-method, go through it and delete the various objects first.
771
+ # If autodelete is set by 'has_many'-method, go through it and delete the various objects first.
781
772
  if autodelete_data = object.class.autodelete_data
782
773
  autodelete_data.each do |adel_data|
783
- self.list(adel_data[:classname], {adel_data[:colname].to_s => object.id}) do |obj_del|
784
- self.delete(obj_del, args)
774
+ list(adel_data[:classname], adel_data[:colname].to_s => object.id) do |obj_del|
775
+ delete(obj_del, args)
785
776
  end
786
777
  end
787
778
  end
788
779
 
789
- #If depend is set by 'has_many'-method, check if any objects exists and raise error if so.
780
+ # If depend is set by 'has_many'-method, check if any objects exists and raise error if so.
790
781
  if dep_datas = object.class.depending_data
791
782
  dep_datas.each do |dep_data|
792
- if obj = self.get_by(dep_data[:classname], {dep_data[:colname].to_s => object.id})
783
+ if obj = get_by(dep_data[:classname], dep_data[:colname].to_s => object.id)
793
784
  raise "Cannot delete <#{object.class.name}:#{object.id}> because <#{obj.class.name}:#{obj.id}> depends on it."
794
785
  end
795
786
  end
796
787
  end
797
788
 
798
- #If autozero is set by 'has_many'-method, check if any objects exists and set the ID to zero.
789
+ # If autozero is set by 'has_many'-method, check if any objects exists and set the ID to zero.
799
790
  if autozero_datas = object.class.autozero_data
800
791
  autozero_datas.each do |zero_data|
801
- self.list(zero_data[:classname], {zero_data[:colname].to_s => object.id}) do |obj_zero|
792
+ list(zero_data[:classname], zero_data[:colname].to_s => object.id) do |obj_zero|
802
793
  obj_zero[zero_data[:colname].to_sym] = 0
803
794
  end
804
795
  end
805
796
  end
806
797
 
807
- #Delete any translations that has been set on the object by 'has_translation'-method.
798
+ # Delete any translations that has been set on the object by 'has_translation'-method.
808
799
  if object.class.translations
809
800
  begin
810
801
  _hb.trans_del(object)
@@ -814,20 +805,20 @@ class Baza::ModelHandler
814
805
  end
815
806
 
816
807
 
817
- #If a buffer is given in arguments, then use that to delete the object.
818
- if args and buffer = args[:db_buffer]
819
- buffer.delete(object.table, {:id => obj_id})
808
+ # If a buffer is given in arguments, then use that to delete the object.
809
+ if args && buffer = args[:db_buffer]
810
+ buffer.delete(object.table, id: obj_id)
820
811
  else
821
- @args[:db].delete(object.table, {:id => obj_id})
812
+ @args[:db].delete(object.table, id: obj_id)
822
813
  end
823
814
 
824
815
  @ids_cache[classname].delete(obj_id.to_i) if @ids_cache_should.key?(classname)
825
- self.call("object" => object, "signal" => "delete")
816
+ call("object" => object, "signal" => "delete")
826
817
  object.destroy
827
- return nil
818
+ nil
828
819
  end
829
820
 
830
- #Deletes several objects as one. If running datarow-mode it checks all objects before it starts to actually delete them. Its faster than deleting every single object by itself...
821
+ # Deletes several objects as one. If running datarow-mode it checks all objects before it starts to actually delete them. Its faster than deleting every single object by itself...
831
822
  def deletes(objs)
832
823
  tables = {}
833
824
 
@@ -836,47 +827,45 @@ class Baza::ModelHandler
836
827
  next if obj.deleted?
837
828
  tablen = obj.table
838
829
 
839
- if !tables.key?(tablen)
840
- tables[tablen] = []
841
- end
830
+ tables[tablen] = [] unless tables.key?(tablen)
842
831
 
843
832
  tables[tablen] << obj.id
844
833
  obj.delete if obj.respond_to?(:delete)
845
834
 
846
- #Remove from ID-cache.
835
+ # Remove from ID-cache.
847
836
  classname = obj.class.classname.to_sym
848
837
  @ids_cache[classname].delete(obj.id.to_i) if @ids_cache_should.key?(classname)
849
838
 
850
- #Unset any data on the object, so it seems deleted.
839
+ # Unset any data on the object, so it seems deleted.
851
840
  obj.destroy
852
841
  end
853
842
  ensure
854
- #An exception may occur, and we should make sure, that objects that has gotten 'delete' called also are deleted from their tables.
843
+ # An exception may occur, and we should make sure, that objects that has gotten 'delete' called also are deleted from their tables.
855
844
  tables.each do |table, ids|
856
845
  ids.each_slice(1000) do |ids_slice|
857
- @args[:db].delete(table, {:id => ids_slice})
846
+ @args[:db].delete(table, id: ids_slice)
858
847
  end
859
848
  end
860
849
  end
861
850
  end
862
851
 
863
- #Deletes all objects with the given IDs 500 at a time to prevent memory exhaustion or timeout.
852
+ # Deletes all objects with the given IDs 500 at a time to prevent memory exhaustion or timeout.
864
853
  #===Examples
865
854
  # ob.delete_ids(:class => :Person, :ids => [1, 3, 5, 6, 7, 8, 9])
866
855
  def delete_ids(args)
867
- while !args[:ids].empty? and ids = args[:ids].shift(500)
868
- objs = self.list(args[:class], "id" => ids)
869
- self.deletes(objs)
856
+ while !args[:ids].empty? && (ids = args[:ids].shift(500))
857
+ objs = list(args[:class], "id" => ids)
858
+ deletes(objs)
870
859
  end
871
860
 
872
- return nil
861
+ nil
873
862
  end
874
863
 
875
- #Try to clean up objects by unsetting everything, start the garbagecollector, get all the remaining objects via ObjectSpace and set them again. Some (if not all) should be cleaned up and our cache should still be safe... dirty but works.
864
+ # Try to clean up objects by unsetting everything, start the garbagecollector, get all the remaining objects via ObjectSpace and set them again. Some (if not all) should be cleaned up and our cache should still be safe... dirty but works.
876
865
  def clean(classn)
877
866
  if classn.is_a?(Array)
878
867
  classn.each do |realclassn|
879
- self.clean(realclassn)
868
+ clean(realclassn)
880
869
  end
881
870
 
882
871
  return nil
@@ -891,15 +880,15 @@ class Baza::ModelHandler
891
880
  @objects[classn] = {}
892
881
  GC.start
893
882
 
894
- @objects.keys.each do |classn|
895
- data = @objects[classn]
896
- classobj = @args[:module].const_get(classn)
883
+ @objects.keys.each do |classname|
884
+ data = @objects[classname]
885
+ classobj = @args[:module].const_get(classname)
897
886
  ObjectSpace.each_object(classobj) do |obj|
898
887
  begin
899
888
  data[obj.id.to_i] = obj
900
889
  rescue => e
901
890
  if e.message == "No data on object."
902
- #Object has been unset - skip it.
891
+ # Object has been unset - skip it.
903
892
  next
904
893
  end
905
894
 
@@ -910,12 +899,12 @@ class Baza::ModelHandler
910
899
  end
911
900
  end
912
901
 
913
- #Erases the whole cache and regenerates it from ObjectSpace if not running weak-link-caching. If running weaklink-caching then it will only removes the dead links.
902
+ # Erases the whole cache and regenerates it from ObjectSpace if not running weak-link-caching. If running weaklink-caching then it will only removes the dead links.
914
903
  def clean_all
915
- self.clean(@objects.keys)
904
+ clean(@objects.keys)
916
905
  end
917
906
 
918
907
  def classes_loaded
919
- return @objects.keys
908
+ @objects.keys
920
909
  end
921
910
  end