Suggestions
Table of Contents
- CRUD
- Opt in over opt out
- Function naming
- Ways of generating names
- Move function naming inside context to delegateall
- Extensibility
- Developer experience
- Adding an example app
CRUD
Opt in over opt out
Instead of using `exclude`, explicitly state functinalities
[include: [:get, :create, :delete]]
This will lead to smaller compiled binaries(due to reducing dead code generated by macro) and to more clarity(devs will know at glance what functions are inside the subcontext)
Function naming
Ways of generating names
Instead of generating functions with names like `list_posts` or `get_post` like this: https://github.com/curiosum-dev/contexted/blob/main/lib/contexted/crud.ex#L67
Allow the user to pass in something like
[naming_scheme: :prefix]
[naming_scheme: :suffix]
[naming_scheme: &String.to_atom("${&1}_${&2}${&3}")] # where &1 is action(get, list) and &2 is resource name and &3 is resource name plural
Move function naming inside context to delegate_all
Instead of forcing users to follow a scheme of naming things like `list_users`. Allow them to call functions as they want to inside the subcontext: e.g `list`
So instead of using `delegate list_users, to: MyApp.Account.Users` use `delegate list_users, to: MyApp.Account.Users, as: :list`. This will make managing name changes of resources and prevent someone from creating `list_posts` inside `MyApp.Account.Users` as it would be exposed as `list_posts_users`
Extensibility
Instead of having a huge quote block https://github.com/curiosum-dev/contexted/blob/main/lib/contexted/crud.ex#L58
Refactor each action into a separate module such as `Subcontexted.Action.Get`
Like this:
defmodule Contexted.Action.Get do
defmacro __using__(opts) do
quote do
def get(...) do
...
end
end
end
end
And then include actions based on the [include: [:get]]
Further functionalities would be easier to implement with this approach such as allowing users to define their own actions
Developer experience
Developer experience could be further improved by using https://github.com/ash-project/spark which would provide great tooling for:
- better automated docs
- LSP integration
- better errors when passing wrong options to `use` macros
The LSP in particular would make using the library easier. For example
defmodule MyApp.Account.Users do
use Contexted, naming_scheme: # <TAB> autocomplete
end
The tab autocomplete then would list `:prefix` and `:suffix`
Adding an example app
Adding an example app using `Contexted` and hosting the generated exdoc would make it easier to understand the idea behind the project