# File lib/dm-core/collection.rb, line 250 def last(*args) last_arg = args.last limit = args.first if args.first.kind_of?(Integer) with_query = last_arg.respond_to?(:merge) && !last_arg.blank? query = with_query ? last_arg : {} query = self.query.slice(0, limit || 1).update(query).reverse! # tell the Query to prepend each result from the adapter query.update(:add_reversed => !query.add_reversed?) # TODO: when a query provided, and there are enough elements in tail to # satisfy the query.limit, filter the tail with the query, and make # sure it matches the limit exactly. if so, use that result instead # of calling all() collection = if !with_query && (loaded? || lazy_possible?(tail, query.limit)) new_collection(query, super(query.limit)) else all(query) end if limit collection else collection.to_a.last end end