-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert
BaseSerializer
into a module
- Loading branch information
Showing
12 changed files
with
115 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Mixins::Serializer | ||
enum Status | ||
Success | ||
Failure | ||
Error | ||
end | ||
|
||
private abstract def status : Status | ||
|
||
macro included | ||
include Lucille::Serializer | ||
|
||
@message : String? | ||
|
||
def render : NamedTuple | ||
json = {status: status} | ||
@message.try { |message| json = json.merge({message: message}) } | ||
data_json.empty? ? json : json.merge({data: data_json}) | ||
end | ||
|
||
private def data_json : NamedTuple | ||
NamedTuple.new | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module Mixins::SuccessSerializer | ||
macro included | ||
include Mixins::Serializer | ||
|
||
@pages : Lucky::Paginator? | ||
|
||
def render : NamedTuple | ||
json = previous_def | ||
|
||
@pages.try do |pages| | ||
json = json.merge({pages: { | ||
current: pages.page, | ||
items: { | ||
count: pages.per_page, | ||
end: pages.item_range.end, | ||
start: pages.item_range.begin, | ||
total: pages.item_count, | ||
}, | ||
next: pages.next_page, | ||
previous: pages.previous_page, | ||
series: pages_series(pages), | ||
total: pages.total, | ||
}}) | ||
end | ||
|
||
json | ||
end | ||
|
||
def self.list(list : Array, *args, **named_args) | ||
list.map { |item| item(item, *args, **named_args) } | ||
end | ||
|
||
private def status : Status | ||
Status::Success | ||
end | ||
|
||
# The idea here is to free the frontend from having to calculate | ||
# pagination parameters itself. We are therefore sending along the | ||
# pagination series as built by *Lucky*. | ||
|
||
# Here, a negative number represents the current page, a `0` | ||
# represents a gap. Eg: `[1,0,5,-6,7,0,11]`. | ||
private def pages_series(pages) | ||
pages.series( | ||
begin: 1, | ||
end: 1, | ||
left_of_current: 2, | ||
right_of_current: 2 | ||
).map do |item| | ||
case item | ||
in Lucky::Paginator::CurrentPage | ||
-item.number | ||
in Lucky::Paginator::Page | ||
item.number | ||
in Lucky::Paginator::Gap | ||
0 | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters