lib/knj/objects.rb in knjrbfw-0.0.94 vs lib/knj/objects.rb in knjrbfw-0.0.95

- old
+ new

@@ -1,13 +1,10 @@ class Knj::Objects attr_reader :args, :events, :data, :ids_cache, :ids_cache_should def initialize(args) require "monitor" - require "#{$knjpath}arrayext" - require "#{$knjpath}event_handler" - require "#{$knjpath}hash_methods" @callbacks = {} @args = args @args[:col_id] = :id if !@args[:col_id] @args[:class_pre] = "class_" if !@args[:class_pre] @@ -398,11 +395,11 @@ def get_by(classname, args = {}) classname = classname.to_sym self.requireclass(classname) classob = @args[:module].const_get(classname) - raise "list-function has not been implemented for #{classname}" if !classob.respond_to?("list") + raise "list-function has not been implemented for '#{classname}'." if !classob.respond_to?(:list) args["limit"] = 1 self.list(classname, args) do |obj| return obj end @@ -828,12 +825,19 @@ end end end #Delete any translations that has been set on the object by 'has_translation'-method. - _kas.trans_del(object) if object.class.translations + if object.class.translations + begin + _hb.trans_del(object) + rescue NameError + _kas.trans_del(object) + end + end + #If a buffer is given in arguments, then use that to delete the object. if args and buffer = args[:db_buffer] buffer.delete(object.table, {:id => obj_id}) else @args[:db].delete(object.table, {:id => obj_id}) @@ -881,9 +885,21 @@ @args[:db].delete(table, {:id => ids_slice}) end end end end + end + + #Deletes all objects with the given IDs 500 at a time to prevent memory exhaustion or timeout. + #===Examples + # ob.delete_ids(:class => :Person, :ids => [1, 3, 5, 6, 7, 8, 9]) + def delete_ids(args) + while !args[:ids].empty? and ids = args[:ids].shift(500) + objs = self.list(:Person, "id" => ids) + self.deletes(objs) + end + + return nil end #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. def clean(classn) if classn.is_a?(Array) \ No newline at end of file