vic

vic

Asdf Core Team

Deco - minimal function decorators with around advice

Yep, yet another library for function decorators in Elixir. This one however, is smaller and simple

   defp is_authorized(decorated, conn, params) do
      if conn.assigns.user do
        decorated.(conn, params)
      else
        conn
        |> send_resp(401, "unauthorized")
        |> halt()
      end
   end

   deco {Deco.around(is_authorized)} in
   def create(conn, params) do
     ...
   end

Most Liked

tmbb

tmbb

Very cool :sunglasses:

Not in the sense thatbI’d ever use it, but in the sence that it makes me want to use it, even though I probably shouldn’t…

I’ve written a similar thing, which I’ve named Doktor. It’s like you regular old Doc, but younger, cooler and more radiKal. Because it had a K. It allows you to write this:

defmodule My.Module do
  use Doktor, "dok/docstrings_for_my_module.md"
  @moduledok "My.Module"

  @dok "hey/0"
  def hey(), do: "Hey!"

  @dok "hello/1"
  def hello(name), do: "Hello #{name}!"
end

Basically, Doktor takes up an Markdown (MD - :joy:) file annotated in a special way and, extracts docstrings from that file and documents the functions with those docstrings. This kan help deKlutter your modules, because it moves the doKstrings somewhere else. I’m still not sure on how useful this is (or whether it is even a good thing at all), but it’s just so KOOL xD

The way it works is by overwriting the @ operator inside the use macro so that it treats the @dok, @moduledok and @typedok` In a special way. Although it’s completely different, it reminded me of this somehow.

vic

vic

Asdf Core Team

haha you got me, I’m always looking at what things I’m able to do with the Elixir syntax, and yeah, the formatter doesnt like it that much, maybe in could be treated just like do and allow a new line after it.

Hey Doktor looks awesome, I’ve been using just @moduledoc File.read!("README.md") but your tool looks nice. One thing I’ve noticed while running tests from an .md file is, I need to modify the .ex file importing it for the new tests to take place, otherwise it’s already compiled with no changes. How you’d handle that in doktor.

:slight_smile:

OvermindDL1

OvermindDL1

I like this trick of linking with in here! I wonder how the formatter formats that though, hmm…

Testing…

iex(1)> Code.format_string!("""
...(1)>    deco {Deco.around(is_authorized)} in
...(1)>    def create(conn, params) do
...(1)>      ...
...(1)>    end
...(1)> """) |> IO.puts()
deco {Deco.around(is_authorized)} in def(create(conn, params)) do
  ...
end

Aww that’s sad, and no way to fix the formatter for this either currently…

tmbb

tmbb

You can use this module attribute: https://hexdocs.pm/elixir/Module.html#module-external_resource

In the case of Doktor, because I’m doing funny things in the use macro (like overwriting @/1) I can add the attribute automatically (I’m not doing it yet, but I can add it), but you’ll have to add it manually with your approach. Mix will recompile your module each time the external file changes. People should always use this attribute when their modules depend on an external file.

In your case, it’s just a question of:

@external_resource "README.md"
@moduledoc File.read!("README.md")

But are you serious about Doktor? Would you use this? xD

I’m not sure I like overwriting the @/1 operator that much, even though it looks nice, because it reuses the familiar pattern of documenting a function or a module. I haven’t released it now because the current implementation is very inefficient. I parse the file everytime I want to document a function. I should start an Agent when the module is compiled, gather all documentation snippets into the agent, have my modified @/1 macro retrieve the docstrings from the agent and kill it after compilation. Compilation of a module is probably done by the same process (I haven’t checked), so this will not be a bottleneck.

Maybe Docktor should use normal functions like

moduledok "My.Module"

dok "hey/1"
def hey(name), do: "hey #{name}"

But last time I tried this there were some small problems of things not being defined before other things. I have to try again. And I do like writing @dok

tmbb

tmbb

I’ve released Doktor (not public on hex yet, or maybe ever): Doktor: extract docs from external files

It now has a much better design. It still doesn’t use the @external_resource thing. I should add it…

Where Next?

Popular in Libraries Top

tompave
Hello there, I would like to share a feature toggles library (AKA feature flags) I’ve been working on. The main package is FunWithFlags...
New
deadtrickster
I’ve just released stable versions of my Prometheus Elixir libs: Elixir client [docs]; Ecto collector [docs]; Plugs instrumenter/Export...
New
mathieuprog
Hello :wave: Allow me to introduce you to Tz, an alternative time zone database support to Tzdata. Why another library? First and fore...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
bryanjos
Hi, I just published version 0.23.0 of Elixirscript. Most of the changes are around JavaScript interop now that Elixirscript uses the ...
New
mtrudel
Bandit is an HTTP server for Plug and WebSock apps. Bandit is written entirely in Elixir and is built atop Thousand Island. It can serve...
New
mattludwigs
Grizzly is a library for working with Z-Wave devices. Z-Wave is a low-frequency radio protocol for controlling smart home devices on a me...
New
bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
381 12391 119
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
axelson
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...
239 45766 226
New
freewebwithme
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Sub Categories:

We're in Beta

About us Mission Statement