lib/spiderfw/model/query_set.rb in spiderfw-0.5.19 vs lib/spiderfw/model/query_set.rb in spiderfw-0.6.0

- old
+ new

@@ -26,11 +26,11 @@ # The BaseModel attr_accessor :model # Total number of objects present in the Storage for the Query attr_accessor :total_rows # (bool) Wether the QuerySet has been loaded - attr_accessor :loaded + attr_reader :loaded # (Fixnum) How many objects to load at a time. If nil, all the objects returned by the Query # will be loaded. attr_accessor :fetch_window # (Fixnum) How many objects to keep in memory when advancing the window. If nil, all objects will be kept. attr_accessor :keep_window @@ -48,10 +48,11 @@ attr_accessor :append_element # (bool) If false, prevents the QuerySet from loading. attr_accessor :loadable # :nodoc: TODO: remove? # Don't put this queryset's objects into the IdentityMapper attr_accessor :_no_identity_mapper + attr_accessor :modified # Instantiates a non-autoloading queryset def self.static(model, query_or_val=nil) qs = self.new(model, query_or_val) qs.autoload = false @@ -170,10 +171,11 @@ @fixed.each do |key, val| obj.set(key, val) end index_object(obj) @raw_data[@objects.length-1] = raw if raw + @modified = true end # Accesses an object. Data will be loaded according to fetch_window. def [](index) @@ -219,10 +221,18 @@ end end @loaded_elements.merge!(f_loaded) end + def modified? + return true if @modified + @objects.each do |obj| + return true if obj.modified? + end + return false + end + # Returns the last object. def last load unless (@loaded || !autoload?) && loaded?(total_rows-1) @objects.last end @@ -230,10 +240,14 @@ # Deletes object at the given index. def delete_at(index) @objects.delete_at(index) end + def delete(obj) + @objects.delete(obj) + end + # Returns a new QuerySet containing objects from both this and the other. def +(other) qs = self.clone other.each do |obj| qs << obj @@ -454,10 +468,11 @@ @loaded = false @loaded_elements = {} return load_next if @fetch_window && !@query.offset mapper.find(@query.clone, self) @loaded = true + @modified = false return self end # Start for the query to get index i def start_for_index(i) # :nodoc: @@ -496,9 +511,14 @@ def loaded?(index=nil) return @loaded if !@loaded || !index || !@fetch_window return false unless @window_current_start return true if index >= @window_current_start-1 && index < @window_current_start+@fetch_window-1 return false + end + + def loaded=(val) + @loaded = val + @modified = false if @loaded end def loadable? @loadable end