I just released the first version of TokenOperator - A dependency-free helper most commonly used for making clean keyword APIs to Phoenix context functions.
I would love feedback/questions on the pattern, the code, issues, better ways to accomplish, etc. This is my first Elixir package so there are probably issues. Thanks!
https://github.com/baldwindavid/token_operator
Here is a quick intro from the documentation:
One thing I’ve struggled with dealing with Phoenix contexts is knowing how to specify the queries to make from the controller. For example, say we want to see a list of blog posts. Sometimes we want that list paginated, sometimes only published, sometimes authors, sometimes with content, sometimes ordered by published date, etc.
We can always just create a bunch of functions on the context for every single variation. Here is an extremely contrived example for illustration:
Posts.list_published_posts_with_author_ordered_by_published_date_paginated(page: 7)
It would be nice to have a simple way to have an API with preset defaults similar to the following:
Posts.list_posts(
filter: [:featured, :published],
include: :author,
paginate: true,
page: 7,
order_by: :publish_date
)
TokenOperator
makes it easy to develop a keyword-based API such as this, using the keywords that make sense for your application. The most obvious use case relates to operating on an Ecto query, but it can operate on any token and has no dependencies.