Umbra v0.0.4 Umbra.Operations View Source

This modules is the facade for the Umbra.CodeGenerator module.

It defines all macros definit/2, defcall/3, defcast/3, definfo/3 and defcontinue/3.

Link to this section Summary

Functions

Generate the GenServer GenServer.handle_call/3 callback and a client method to call the function through GenServer.call/2.

Generate the GenServer GenServer.handle_cast/2 callback and a client method to call the function through GenServer.cast/2.

Generate the GenServer GenServer.handle_info/2 callback and a client method to call the function through Process.send/3.

Link to this section Functions

Link to this macro

defcall(definition, opts \\ [], body \\ [])

View Source (macro)

Specs

defcall(atom() | tuple(), list(), list()) :: tuple()

Generate the GenServer GenServer.handle_call/3 callback and a client method to call the function through GenServer.call/2.

By default generates the client and server function in public.

Options

  • private: boolean(), default to false
  • when: a statement
  • server: boolean(), default to true
  • client: boolean(), default to true
  • state: a statement, default to _state
  • from: a statement, default to _from

Example

Defining:

defcall {:compute, a, b}, do: {:reply, a + b, nil}

Will generate:

def get_state(pid_or_state, a, b) do
  {:ok, GenServer.call(pid_or_state, {:compute, a, b})
end
def handle_call({:compute, a, b}, _from, _state) do
  {:reply, a + b, nil}
end
Link to this macro

defcast(definition, opts \\ [], body \\ [])

View Source (macro)

Specs

defcast(atom() | tuple(), list(), list()) :: tuple()

Generate the GenServer GenServer.handle_cast/2 callback and a client method to call the function through GenServer.cast/2.

By default generates the client and server function in public.

Options

  • private: boolean(), default to false
  • when: a statement
  • server: boolean(), default to true
  • client: boolean(), default to true
  • state: a statement, default to _state

Example

Defining:

defcast {:set_state, %{id: id, name: name} = new_state}, do: {:noreply, new_state}

Will generate:

def set_state(pid, %{id: _id, name: _name} = new_state) do
  GenServer.cast(pid, {:set_state, new_state})
end
def handle_cast({:set_state, %{id: id, name: name} = new_state}, _state) do
  {:noreply, new_state}
end
Link to this macro

defcontinue(definition, opts \\ [], body \\ [])

View Source (macro)

Specs

defcontinue(atom() | tuple(), list(), list()) :: tuple()

Generate the GenServer GenServer.handle_continue/2 callback.

It is server-side only, so no client method will be defined.

Options

  • when: a statement
  • state: a statement, default to _state

Example

Defining:

defcontinue {:send_to_process, pid, result}, state: state do
  Process.send(pid, result)
  {:noreply, state}
end

Will generate:

def handle_continue({:send_to_process, pid, result}, state) do
  Process.send(pid, result)
  {:noreply, state}
end
Link to this macro

definfo(definition, opts \\ [], body \\ [])

View Source (macro)

Specs

definfo(atom() | tuple(), list(), list()) :: tuple()

Generate the GenServer GenServer.handle_info/2 callback and a client method to call the function through Process.send/3.

By default only generate the server-side function. The client-side function can be useful sometimes.

Options

  • private: boolean(), default to false
  • when: a statement
  • server: boolean(), default to true
  • client: boolean(), default to false
  • state: a statement, default to _state

Example

Defining:

definfo {:ping}, client: true, state: state do
  IO.puts(:pong)
  {:noreply, state}
end

Will generate:

def ping(pid) do
  Process.send(pid, {:ping})
end
def handle_info({:ping}, state) do
  IO.puts(:pong)
  {:noreply, state}
end
Link to this macro

definit(opts \\ [], body \\ [])

View Source (macro)

Specs

definit(list(), list()) :: tuple()

Generate the GenServer GenServer.init/1 callback.

It is server-side only, so no client method will be defined.

Options

  • when: a statement
  • state: a statement, default to _state

Example

Defining:

definit state: state, do: {:ok, state}

Will generate:

def init(state: state) do
  {:ok, state}
end