# File lib/dm-core/collection.rb, line 205 def first(*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) # TODO: when a query provided, and there are enough elements in head to # satisfy the query.limit, filter the head with the query, and make # sure it matches the limit exactly. if so, use that result instead # of calling all() # - this can probably only be done if there is no :order parameter collection = if !with_query && (loaded? || lazy_possible?(head, query.limit)) new_collection(query, super(query.limit)) else all(query) end if limit collection else collection.to_a.first end end