def first(*args)
first_arg = args.first
last_arg = args.last
limit_specified = first_arg.kind_of?(Integer)
with_query = (last_arg.kind_of?(Hash) && !last_arg.empty?) || last_arg.kind_of?(Query)
limit = limit_specified ? first_arg : 1
query = with_query ? last_arg : {}
query = self.query.slice(0, limit).update(query)
loaded = loaded?
head = self.head
collection = if !with_query && (loaded || lazy_possible?(head, limit))
new_collection(query, super(limit))
else
all(query)
end
return collection if limit_specified
resource = collection.to_a.first
if with_query || loaded
resource
elsif resource
head[0] = resource
end
end