Skip to content

Commit

Permalink
Merge pull request #5 from stephenmoloney/update/scrivener-2
Browse files Browse the repository at this point in the history
Upgrade to allow for scrivener version 2.x
  • Loading branch information
stephenmoloney authored Feb 11, 2019
2 parents b2c7b80 + d535ea8 commit 98abfd1
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 126 deletions.
6 changes: 6 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
inputs: [
"{lib,config,test}/**/*.{ex,exs}",
"*.{ex,exs}"
]
]
68 changes: 68 additions & 0 deletions .remarkrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"plugins": [
["remark-lint-checkbox-character-style", "consistent"],
["remark-lint-final-newline"],
["remark-lint-list-item-bullet-indent", "tab-size"],
["remark-lint-list-item-indent", "tab-size"],
["remark-lint-no-auto-link-without-protocol"],
["remark-lint-no-blockquote-without-marker"],
["remark-lint-no-literal-urls"],
["remark-lint-ordered-list-marker-style", "."],
["remark-lint-hard-break-spaces"],
["remark-lint-no-duplicate-definitions"],
["remark-lint-no-heading-content-indent"],
["remark-lint-no-inline-padding"],
["remark-lint-no-shortcut-reference-image"],
["remark-lint-no-shortcut-reference-link"],
["remark-lint-no-undefined-references"],
["remark-lint-no-unused-definitions"],
["remark-lint-file-extension", "md"],
["remark-lint-no-file-name-mixed-case"],
["remark-lint-no-file-name-articles"],
["remark-lint-no-file-name-irregular-characters"],
["remark-lint-no-file-name-consecutive-dashes"],
["remark-lint-no-file-name-outer-dashes"],
["remark-lint-no-consecutive-blank-lines"],
["remark-lint-maximum-line-length", 100],
["remark-lint-no-shell-dollars"],
["remark-lint-heading-style", "atx"],
["remark-lint-heading-increment"],
["remark-lint-no-duplicate-headings"],
["remark-lint-no-multiple-toplevel-headings"],
["remark-lint-maximum-heading-length"],
["remark-lint-no-heading-punctuation", ":."],
["remark-lint-blockquote-indentation", 2],
["remark-lint-unordered-list-marker-style", "-"],
["remark-lint-ordered-list-marker-value", "one"],
["remark-lint-list-item-content-indent"],
["remark-lint-list-item-spacing"],
["remark-lint-code-block-style", "fenced"],
["remark-lint-fenced-code-flag", {"allowEmpty": false}],
["remark-lint-fenced-code-marker", "`"],
["remark-lint-rule-style", "---"],
["remark-lint-no-table-indentation"],
["remark-lint-table-pipes"],
["remark-lint-table-pipe-alignment"],
["remark-lint-table-cell-padding", "padded"],
["remark-lint-final-definition"],
["remark-lint-definition-case"],
["remark-lint-definition-spacing"],
["remark-lint-link-title-style", "\""],
["remark-lint-strong-marker", "*"],
["remark-lint-emphasis-marker", "*"],
["remark-lint-no-emphasis-as-heading", "*"],
["remark-lint-no-dead-urls"],
["remark-validate-links"],
["remark-lint-no-url-trailing-slash"],
[
"remark-lint-write-good",
[
"warn",
{
"passive": false,
"so": true
}
]
]
]
}
67 changes: 61 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,62 @@
language: elixir
matrix:
---
env:
global:
- WORKSPACE="/ci-actions/workspace"
- REMARKLINT_VERSION="6.0.4"

stages:
- markdown-lint
- elixir-lint
- test-1.3
- test-1.4
- test-1.5
- test-1.6
- test-1.7
- test-1.8

jobs:
include:
- otp_release: 19.2
elixir: 1.3
- otp_release: 19.3
elixir: 1.4
- stage: markdown-lint
language: bash
image: docker:18-git
before_script:
- docker pull smoloney/ci-actions-remark:${REMARKLINT_VERSION} > /dev/null
script:
- |
docker run --rm -v ${PWD}:${WORKSPACE} smoloney/ci-actions-remark:${REMARKLINT_VERSION} \
--exclude='./deps' \
--exclude='./_build' \
--exec-args='--frail --rc-path ./.remarkrc'
- stage: elixir-lint
language: elixir
elixir: 1.8
otp_release: 21.2
script:
- mix format --check-formatted --dry-run

- &test
stage: test-1.4
language: elixir
elixir: 1.4
otp_release: 19.2

- <<: *test
stage: test-1.5
elixir: 1.5
otp_release: 20.3

- <<: *test
stage: test-1.6
elixir: 1.6
otp_release: 20.3

- <<: *test
stage: test-1.7
elixir: 1.7
otp_release: 20.3

- <<: *test
stage: test-1.8
elixir: 1.8
otp_release: 21.2
29 changes: 23 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
# Changelog

## v1.0.1
## v2.0.0

- Relax the requirements for `scrivener_ecto` to allow 1.x and 2.x
versions - thereby also allowing ecto 3.x,
resolves [issue 4](https://github.com/stephenmoloney/scrivener_list/issues/4)

- README example without a Repo module no longer references
a Repo module confusingly - [PR](https://github.com/stephenmoloney/scrivener_list/pull/2)

- Formatting introduced with `mix format`

[fixes]
- Cannot pass %Scrivener.Config.t into `Scrivener.Repo.paginate`. Change docs to reflect this.
- Add `:scrivener_ecto` to `applications` and hence quieten warnings for releases.
- bump dependencies & fix warnings for elixir 1.4.
- Linting the `README.md` file

- Add example in `README.md` of adding `adapter: Ecto.Adapters.Postgres` in the
`use` statement when creating a `Repo` module as is required
for Ecto 3+.

- Elixir 1.4+ is required for scrivener_ecto 2.x

## v1.0.1

- Cannot pass %Scrivener.Config.t into `Scrivener.Repo.paginate`. Change docs to reflect this.
- Add `:scrivener_ecto` to `applications` and hence quieten warnings for releases.
- bump dependencies & fix warnings for elixir 1.4.

## v1.0

- Initial release.
- Initial release.
68 changes: 35 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ allows the pagination of a list of elements.

## Features

1. Scrivener.List extends the protocol `Scrivener.Paginater.paginate/2` from the [scrivener](https://github.com/drewolson/scrivener) library.
2. Scrivener.List also extends the function `MyApp.Repo.paginate/2` from the [scrivener_ecto](https://github.com/drewolson/scrivener_ecto) library.
1. Scrivener.List extends the protocol `Scrivener.Paginater.paginate/2` from the [scrivener](https://github.com/drewolson/scrivener) library.
1. Scrivener.List also extends the function `MyApp.Repo.paginate/2` from the [scrivener_ecto](https://github.com/drewolson/scrivener_ecto) library.

Using the second feature is entirely optional. It is provided as a convenience where the [scrivener_ecto](https://github.com/drewolson/scrivener_ecto)
library is also being used in the project and gives access to the pre-configured `MyApp.Repo` module.

Using the second feature is entirely optional. It's provided as a convenience where the [scrivener_ecto](https://github.com/drewolson/scrivener_ecto)
library is also being used in the project and gives access to the
pre-configured `MyApp.Repo` module.

## Usage

## 1. Usage without a Repo module

```elixir
Scrivener.paginate(list, config)
```

### Function

```Scrivener.paginate(list, config)```
### Arguments (without a repo)

### Arguments
- ```list```: A list of elements to be paginated

- ```list```: A list of elements to be paginated
- ```config```: A configuration object with the pagination details. Can be in any of the following formats:
- ```%{page: page_number, page_size: page_size}``` (map)
- ```[page: page_number, page_size: page_size]``` (Keyword.t)
- ```%Scrivener.Config{page_number: page_number, page_size: page_size}``` (Scrivener.Config.t)
- ```config```: A configuration object with the pagination details.
Can be in any of the following formats:
- ```%{page: page_number, page_size: page_size}``` (map)
- ```[page: page_number, page_size: page_size]``` (Keyword.t)
- ```%Scrivener.Config{page_number: page_number, page_size: page_size}``` (Scrivener.Config.t)

`max_page_size` **cannot** be configured with method 1.

Expand Down Expand Up @@ -55,8 +56,7 @@ defp maybe_put_default_config(%{page: page_number, page_size: page_size} = param
defp maybe_put_default_config(_params), do: %Scrivener.Config{page_number: 1, page_size: 10}
```


```elixir
```elixir
list = ["C#", "C++", "Clojure", "Elixir", "Erlang", "Go", "JAVA", "JavaScript", "Lisp",
"PHP", "Perl", "Python", "Ruby", "Rust", "SQL"]

Expand All @@ -66,7 +66,6 @@ Scrivener.paginate(list, %{page: 1, page_size: 4}) # map
Scrivener.paginate(list, %{page: 1}) # map with only page number (page_size defaults to 10)
```


## 2. Usage with a Repo module

Usage without a Repo is entirely optional and is added to `Scrivener.List` for convenience.
Expand All @@ -76,29 +75,34 @@ Firstly, see [Scrivener.Ecto](https://github.com/drewolson/scrivener_ecto) and c

```elixir
defmodule MyApp.Repo do
use Ecto.Repo, otp_app: :my_app
use Scrivener, page_size: 10, max_page_size: 100
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres
use Scrivener,
page_size: 10,
max_page_size: 100
end
```

### Function
```elixir
MyApp.Repo.paginate(list, config)
```

```MyApp.Repo.paginate(list, config)```
### Arguments (with a repo)

### Arguments
- ```list```: A list of elements to be paginated

- ```list```: A list of elements to be paginated
- ```config```: A configuration object with the pagination details. Can be in any of the following formats:
- ```%{page: page_number, page_size: page_size}``` (map)
- ```[page: page_number, page_size: page_size]``` (Keyword.t)
- ```config```: A configuration object with the pagination details.
Can be in any of the following formats:
- ```%{page: page_number, page_size: page_size}``` (map)
- ```[page: page_number, page_size: page_size]``` (Keyword.t)

`max_page_size` **can** be configured with method 1. See [Scrivener.Ecto](https://github.com/drewolson/scrivener_ecto).

### Example

Example based on [scrivener_ecto](https://github.com/drewolson/scrivener_ecto) readme.


```elixir
def index(conn, params) do
%{age: age, name: name} = params["search"]
Expand All @@ -120,7 +124,6 @@ def index(conn, params) do
end
```


## Installation

Add [scrivener_list](https://hex.pm/packages/scrivener_list) to your list of dependencies in `mix.exs`:
Expand All @@ -133,18 +136,17 @@ def deps do
end
```


## Tests

```shell
mix test
MIX_ENV=test mix test
```


## Acknowledgements

Thanks to [Drew Olson](https://github.com/drewolson) for helping with this, particularly the introduction of the [protocol](http://blog.drewolson.org/extensible-design-with-protocols/).

The introduction of the [protocol](http://blog.drewolson.org/extensible-design-with-protocols/)
for scrivener enabled the separation of this package into it's own
repository.

## Licence

Expand Down
7 changes: 3 additions & 4 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use Mix.Config

if Mix.env == :test do
config :scrivener_list, Scrivener.Repo,
adapter: Ecto.Adapters.Postgres
end
if Mix.env() == :test do
config :scrivener_list, Scrivener.Repo, adapter: Ecto.Adapters.Postgres
end
31 changes: 14 additions & 17 deletions lib/scrivener/paginater/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,43 @@ defimpl Scrivener.Paginater, for: List do
alias Scrivener.Config
alias Scrivener.Page


@doc ~s"""
Implementation of `Scrivener.Paginater` protocol to extend the `Scrivener.Paginater.paginate/2` function.
"""
@spec paginate(any, map | Keyword.t) :: Scrivener.Page.t
@spec paginate(any, map | Keyword.t()) :: Scrivener.Page.t()
def paginate(entries, %Config{page_number: page_number, page_size: page_size}) do
total_entries = length(entries)

%Page{
page_size: page_size,
page_number: page_number,
entries: entries(entries, page_number, page_size),
total_entries: total_entries,
total_pages: total_pages(total_entries, page_size)
}
total_entries = length(entries)

%Page{
page_size: page_size,
page_number: page_number,
entries: entries(entries, page_number, page_size),
total_entries: total_entries,
total_pages: total_pages(total_entries, page_size)
}
end


defp entries(entries, page_number, page_size) do
offset = page_size * (page_number - 1)
Enum.slice(entries, offset, page_size)
end


defp total_pages(total_entries, page_size) do
ceiling(total_entries / page_size)
end


defp ceiling(float) do
t = trunc(float)

case float - t do
neg when neg < 0 ->
t

pos when pos > 0 ->
t + 1
_ -> t

_ ->
t
end
end


end
Loading

0 comments on commit 98abfd1

Please sign in to comment.