thojanssens1
Better way to get default option?
I have something like:
Keyword.get(
opts,
:upload_path_changer,
Application.get_env(
:file_uploader,
:upload_path_changer,
MyApp.UploadPathChanger
)
)
Get the :upload_path_changer module from options (opts);
if not found in options, get it from the config file;
if not found in the config file, use MyApp.UploadPathChanger.
Do you think the code above can improve in readability?
Most Liked
dimitarvp
Just make a helper in one of your modules. I also found such snippets very tiresome and just hid the complexity away in neat quick functions.
6
hauleth
This will change semantics of the original, as opts = [upload_path_changer: nil] will return different results in original and this one.
Maybe merge options with defaults?
default_opts = [
upload_patch_changer: Application.get_env(:file_uploader, :upload_patch_changer, MyApp.UploadPatchChanger)
# other options if needed
]
opts = Keyword.merge(default_opts, opts)
4
NobbZ
Something like this?
Keyword.get(opts, :upload_path_changer) || Application.get_env(:file_uploader, :upload_path_changer) || MyApp.UploadPathChager
3
Kurisu
I really like this approach, especially when there are several default options. ^^
1
Popular in Questions
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
can someone please explain to me how Enum.reduce works with maps
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database.
Dep...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
Other popular topics
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir? ...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New







