aggregate (DataMapper::Aggregates::Functions)

    # File lib/dm-aggregates/aggregate_functions.rb, line 151
          def aggregate(*args)
            query = args.last.kind_of?(Hash) ? args.pop : {}

            query[:fields] ||= []
            query[:fields]  |= args
            query[:fields].map! { |f| normalize_field(f) }
            query[:order]  ||= query[:fields].select { |p| p.kind_of?(Property) }

            raise ArgumentError, 'query[:fields] must not be empty' if query[:fields].empty?

            query = scoped_query(query)

            if query.fields.any? { |p| p.kind_of?(Property) }
              query.repository.aggregate(query.update(:unique => true))
            else
              query.repository.aggregate(query).first  # only return one row
            end
          end