Property definitions

nitc $ CmdList :: defaultinit
# A command that returns a list of results
abstract class CmdList
	super DocCommand

	# Type of result
	type ITEM: Object

	# Limit the items in the list
	var limit: nullable Int = null is optional, writable

	# Page to display
	var page: nullable Int = null is optional, writable

	# Total number of ret
	var count: nullable Int = null is optional, writable

	# Total number of pages
	var max: nullable Int = null is optional, writable

	# Comparator used to sort the list
	var sorter: nullable Comparator = null is writable

	# Items in the list
	var results: nullable Array[ITEM] = null is writable

	# `init_command` is used to factorize the sorting and pagination of results
	#
	# See `init_results` for the result list initialization.
	redef fun init_command do
		var res = super
		if not res isa CmdSuccess then return res
		res = init_results
		if not res isa CmdSuccess then return res
		sort
		paginate
		return res
	end

	# Initialize the `results` list
	#
	# This method must be redefined by CmdList subclasses.
	fun init_results: CmdMessage do return new CmdSuccess

	# Sort `mentities` with `sorter`
	fun sort do
		var results = self.results
		if results == null then return
		var sorter = self.sorter
		if sorter == null then return
		sorter.sort(results)
	end

	# Paginate the results
	#
	# This methods keeps only a subset of `results` depending on the current `page` and the
	# number of elements to return set by `limit`.
	#
	# The `count` can be specified when `results` does not contain all the results.
	# For example when the results are already limited from a DB statement.
	fun paginate do
		var results = self.results
		if results == null then return

		var limit = self.limit
		if limit == null then return

		var page = self.page
		if page == null or page <= 0 then page = 1

		var count = self.count
		if count == null then count = results.length

		var max = count / limit
		if max == 0 then
			page = 1
			max = 1
		else if page > max then
			page = max
		end

		var lstart = (page - 1) * limit
		var lend = limit
		if lstart + lend > count then lend = count - lstart
		self.results = results.subarray(lstart, lend)
		self.max = max
		self.limit = limit
		self.page = page
		self.count = count
	end
end
src/doc/commands/commands_base.nit:217,1--306,3