Skip to content

Commit

Permalink
Update Phoenix.Socket.assign/2,3 docs (#5905)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach authored Oct 22, 2024
1 parent 166a2c9 commit a6328f1
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lib/phoenix/socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -332,24 +332,32 @@ defmodule Phoenix.Socket do
## USER API

@doc """
Adds key-value pairs to socket assigns.
Adds a `key`/`value` pair to `socket` assigns.
A single key-value pair may be passed, a keyword list or map
of assigns may be provided to be merged into existing socket
assigns.
See also `assign/2` to add multiple key/value pairs.
## Examples
iex> assign(socket, :name, "Elixir")
iex> assign(socket, name: "Elixir", logo: "💧")
"""
def assign(%Socket{} = socket, key, value) do
assign(socket, [{key, value}])
end

def assign(%Socket{} = socket, attrs)
when is_map(attrs) or is_list(attrs) do
%{socket | assigns: Map.merge(socket.assigns, Map.new(attrs))}
@doc """
Adds key/value pairs to socket assigns.
A keyword list or a map of assigns must be given as argument to be merged into existing assigns.
## Examples
iex> assign(socket, name: "Elixir", logo: "💧")
iex> assign(socket, %{name: "Elixir"})
"""
def assign(%Socket{} = socket, keyword_or_map)
when is_map(keyword_or_map) or is_list(keyword_or_map) do
%{socket | assigns: Map.merge(socket.assigns, Map.new(keyword_or_map))}
end

@doc """
Expand Down

0 comments on commit a6328f1

Please sign in to comment.