-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add benchmarks for alba and panko_serializer
- Loading branch information
Showing
5 changed files
with
162 additions
and
0 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 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,58 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'alba' | ||
|
||
Alba.backend = :oj | ||
Alba.inflector = :active_support | ||
|
||
class SongAlba | ||
include Alba::Resource | ||
transform_keys :lower_camel | ||
|
||
attributes :id, if: proc { |song| !song.new_record? } | ||
|
||
attributes( | ||
:track, | ||
:name, | ||
) | ||
|
||
attributes | ||
def composers(song) | ||
song.composer&.split(', ') | ||
end | ||
end | ||
|
||
class AlbumAlba | ||
include Alba::Resource | ||
transform_keys :lower_camel | ||
|
||
attributes :id, if: proc { |album| !album.new_record? } | ||
|
||
attributes( | ||
:name, | ||
:genres, | ||
) | ||
|
||
attributes :release, if: proc { |album| album.released? } | ||
def release(album) | ||
album.release_date.strftime('%B %d, %Y') | ||
end | ||
|
||
attributes | ||
def special | ||
options[:special] | ||
end | ||
|
||
one :songs, resource: SongAlba | ||
many :other_songs, resource: SongAlba, if: proc { |album| album.other_songs.present? } | ||
end | ||
|
||
class ModelAlba | ||
include Alba::Resource | ||
transform_keys :lower_camel | ||
|
||
attributes( | ||
:id, | ||
:name, | ||
) | ||
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,81 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'panko_serializer' | ||
|
||
Mongoid::Criteria.prepend Module.new { | ||
def track | ||
end | ||
} | ||
|
||
Album.prepend Module.new { | ||
def other_songs | ||
[] | ||
end | ||
} | ||
|
||
class SongPanko < Panko::Serializer | ||
attributes | ||
def id | ||
object.id unless object.new_record? | ||
end | ||
|
||
attributes( | ||
:track, | ||
:name, | ||
) | ||
|
||
attributes | ||
def composers | ||
object.composer&.split(', ') | ||
end | ||
|
||
# NOTE: Panko does not have a way to conditionally not include an attribute. | ||
def self.filters_for(context, scope) | ||
{ | ||
except: [:id] | ||
} | ||
end | ||
end | ||
|
||
class AlbumPanko < Panko::Serializer | ||
attributes | ||
def id | ||
object.id unless object.new_record? | ||
end | ||
|
||
attributes( | ||
:name, | ||
:genres, | ||
) | ||
|
||
attributes | ||
def release | ||
object.release_date.strftime('%B %d, %Y') if object.released? | ||
end | ||
|
||
attributes | ||
def special | ||
context[:special] | ||
end | ||
|
||
has_one :songs, serializer: SongPanko | ||
has_many :other_songs, serializer: SongPanko | ||
|
||
def other_songs | ||
other_songs if other_songs.present? | ||
end | ||
|
||
# NOTE: Panko does not have a way to conditionally not include an attribute. | ||
def self.filters_for(context, scope) | ||
{ | ||
except: [:id, :special] | ||
} | ||
end | ||
end | ||
|
||
class ModelPanko < Panko::Serializer | ||
attributes( | ||
:id, | ||
:name, | ||
) | ||
end |