Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ornicar/scala-paginator
Generic paginator for scala
https://github.com/ornicar/scala-paginator
Last synced: 9 days ago
JSON representation
Generic paginator for scala
- Host: GitHub
- URL: https://github.com/ornicar/scala-paginator
- Owner: ornicar
- License: mit
- Created: 2011-10-13T18:00:49.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2012-07-03T12:59:19.000Z (over 12 years ago)
- Last Synced: 2023-03-10T21:18:15.762Z (over 1 year ago)
- Language: Scala
- Homepage:
- Size: 116 KB
- Stars: 6
- Watchers: 3
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Generic paginator for scala
- Provides pagination for any data provider: DB, remote API, memory, ...
- Based on a simple adapter pattern.
- Fully functional.## Example
```scala
import com.github.ornicar.paginator._val adapter = InMemoryAdapter('a' to 'z')
for {
paginator <- Paginator(adapter, currentPage = 2, maxPerPage = 5).right
} {
paginator.nbResults // 26
paginator.nbPages // 6
paginator.currentPageResults // Vector('f', 'g', 'h', 'i', 'j')
paginator.currentPage // 2
paginator.maxPerPage // 5
paginator.hasToPaginate // True
paginator.hasNextPage // True
paginator.hasPreviousPage // True
paginator.nextPage // Some(3)
paginator.previousPage // Some(1)
}
```## Adapters
### InMemoryAdapter
This basic implementation of the adapter paginates a `Seq`. Example:
```scala
val adapter = InMemoryAdapter('a' to 'z')val paginator = Paginator(adapter)
````paginator` is an instance of `Either[String, Paginator[A]]` where the left part is a possible error message.
```scala
Paginator(adapter) // Right[Paginator[A]]Paginator(adapter, 1, 10) // Right[Paginator[A]]
Paginator(adapter, 0, 10) // Left("Max per page must be greater than zero")
Paginator(adapter, 1, 0) // Left("Current page must be greater than zero")
```### MongoDB Salat adapter
Allows to paginate on any MongoDB query:
```scala
import com.github.ornicar.paginator.SalatAdapter // I assume you also import the salat stuffcase class Person(name: String) // Say we have a basic model class
object PersonDAO extends SalatDAO[Person, ObjectId](collection) // And a DAO for the model
val query = MongoDBObject("name" -> "Joe") // This is a normal salat query
val adapter = SalatAdapter(PersonDAO, query) // And a shiny mongodb paginator adapter
val paginator = Paginator(adapter)
```The adapter will take care of adding the `skip` and `limit` options
to the mongodb query.
To use this adapter, you must include the `salat-adapter` project.## Create another adapter
To create a new adapter, just implement the 2 methods of this trait:
```scala
trait Adapter[A] {/**
* Returns the number of results.
*/
def nbResults: Int/**
* Returns an slice of the results.
*
* @param offset The number of elements to skip, starting from zero
* @param length The maximum number of elements to return
*/
def slice(offset: Int, length: Int): Seq[A]
}
```You may use the provided `InMemoryAdapter` and `SalatAdapter` as implementation examples.