lib/model_set.rb in ninjudd-model_set-0.10.0 vs lib/model_set.rb in ninjudd-model_set-0.10.1
- old
+ new
@@ -22,21 +22,28 @@
deep_clonable
MAX_CACHE_SIZE = 1000 if not defined?(MAX_CACHE_SIZE)
+ attr_reader :created_at
+
def initialize(query_or_models)
if query_or_models.kind_of?(Query)
@query = query_or_models
elsif query_or_models.kind_of?(self.class)
self.ids = query_or_models.ids
@models_by_id = query_or_models.models_by_id
elsif query_or_models
self.ids = as_ids(query_or_models)
end
+ @created_at = Time.now
end
+ def older_than?(duration)
+ created_at.nil? or created_at < Time.now - duration
+ end
+
def ids
model_ids.to_a
end
def missing_ids
@@ -350,16 +357,19 @@
query.send(method_name, *args)
self
end
end
- [:unsorted!, :limit!, :page!, :unlimited!].each do |method_name|
+ [:unsorted!, :limit!, :page!, :unlimited!, :reverse!].each do |method_name|
clone_method method_name
define_method(method_name) do |*args|
# Don't change the query engine by default
anchor!( extract_opt(:query_type, args) )
+ # Use the default query engine if the the current engine doesn't respond to the method.
+ anchor!(default_query_type) unless query.respond_to?(method_name)
+
query.send(method_name, *args)
self
end
end
@@ -527,14 +537,14 @@
self.class.send(method, *args)
end
end
def marshal_dump
- [ @query, @add_fields, @included_models ]
+ [ @query, @add_fields, @included_models, @created_at ]
end
def marshal_load(fields)
- @query, @add_fields, @included_models = fields
+ @query, @add_fields, @included_models, @created_at = fields
end
protected
def db