Skip to content
This repository has been archived by the owner on Aug 8, 2019. It is now read-only.

Commit

Permalink
Merge pull request phoenixframework#153 from knewter/feature/add_sock…
Browse files Browse the repository at this point in the history
…et_assign_api

Add assign API for Socket
  • Loading branch information
chrismccord committed Jul 4, 2014
2 parents 9466968 + 9cea3da commit f6bdc4f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/phoenix/socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Phoenix.Socket do
topic: nil,
router: nil,
channels: [],
assigns: []
assigns: %{}

def set_current_channel(socket, channel, topic) do
%Socket{socket | channel: channel, topic: topic}
Expand All @@ -28,6 +28,10 @@ defmodule Phoenix.Socket do
def authenticated?(socket, channel, topic) do
Enum.member? socket.channels, {channel, topic}
end

def assign(socket, key, value) do
%Socket{socket | assigns: Map.put(socket.assigns, key, value)}
end
end


9 changes: 8 additions & 1 deletion test/phoenix/socket/socket_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Phoenix.Socket.SocketTest do
%Socket{pid: self,
router: nil,
channels: [],
assigns: []}
assigns: %{}}
end

test "#set_current_channel sets the current channel" do
Expand Down Expand Up @@ -47,5 +47,12 @@ defmodule Phoenix.Socket.SocketTest do
socket = Socket.delete_channel(socket, "test", "topic")
assert socket.channels == []
end

test "#assign assigns into the assigns map (yo dog)" do
socket = new_socket
assert socket.assigns == %{}
socket = Socket.assign(socket, :foo, "bar")
assert socket.assigns == %{foo: "bar"}
end
end

0 comments on commit f6bdc4f

Please sign in to comment.