Hi everyone, I am very new to the Erlang / Elixir / Phoenix scene, so sorry if i’m posting this in the wrong spot, or my terminology is off, feel free to correct me on anything since i’d rather learn.
I’m currently reading and following along with the “Programming Phoenix” book and have a few question about module plugs.
So far, what I understand is that for a module plug to be a ‘module plug’, it needs two functions, init/1 and call/2.
In the book, we make a module called Rumbl.Auth. This module uses the expected init/1 and call/2 functions, but also adds others, such as login/2, login_by_username_and_pass/4 and some others.
Are these ‘other’ functions (login, and login_by_username_and_pass) relying on call/2 in order to operate properly? Or are they completely isolated and ran independently?
Here is the Plug i’m working on… auth module plug
My question is due to me trying to understand when ‘call’ is actually being executed by Phoenix.
I understand I add the module to my router like so
defmodule Rumbl.Router do
use Rumbl.Web, :router
pipeline :browser do plug :accepts, ["html"] plug :fetch_session plug :fetch_flash plug :protect_from_forgery plug :put_secure_browser_headers plug Rumbl.Auth, repo: Rumbl.Repo end
But i’m not sure when ‘call’ is actually getting called since I never explicitly call it. Is it getting called every time I run another function within that module? Or does it get called once? I understand init gets called during compile time to set things up for call, and call/2 gets executed at runtime.
I guess i’m confused why ‘call’ is even needed in this case, and why I can’t just treat the module (other than the fact that it needs call/2 to be considered a plug) as a module. Is call there just for the sake of being called a ‘plug’?
I hope my question makes sense. Sorry if it’s something blatant i’m missing. Excited to read any replies. Been really enjoying Functional / Erlang / Elixir / Phoenix so far… I haven’t been this excited to learn something in a long time.