Skip to content

Commit

Permalink
Allow connection to be updated
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Oct 3, 2012
1 parent 148c0cb commit 3f8f980
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 26 deletions.
10 changes: 8 additions & 2 deletions lib/dynamo/http/hibernate.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
defmodule Dynamo.HTTP.Hibernate do
@assign :__timer_ref

@moduledoc """
Conveniences that allows a connection to hibernate or
sleep a given amount or an unlimited amount of time.
Such conveniences are useful when a connection needs
to be kept open (because of long polling, websockets
or streaming) but you don't want to keep the current
erlang process active all times and sleeping through
small intervals or hibernating through long intervals
is convenient.
"""

@assign :__timer_ref

@doc """
Hibernates the current process until a message is received.
The `on_wake_up` callback is invoked with the `conn` and the
Expand Down
6 changes: 4 additions & 2 deletions lib/dynamo/router/rendering.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ defmodule Dynamo.Router.Rendering do
locals = [conn: conn]
assigns = Keyword.merge(conn.assigns, assigns)
layout = assigns[:layout]
body = Dynamo.View.render(template, locals, assigns)

{ [conn], body } = Dynamo.View.render(template, locals, assigns)

if layout do
template = Dynamo.View.find!("layouts/" <> layout, view_paths)
body = Dynamo.View.render(template, locals, Keyword.put(assigns, :yield, body))
assigns = Keyword.put(assigns, :yield, body)
{ [conn], body } = Dynamo.View.render(template, locals, assigns)
end

conn.resp_body(body)
Expand Down
28 changes: 9 additions & 19 deletions lib/dynamo/view/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,51 +30,41 @@ defmodule Dynamo.View.Handler do
Get the template handler for the given extension.
"""
def get!(extension) do
module = Module.concat(Dynamo.View, upcase(extension) <> "Handler")
module = Module.concat(Dynamo.View, String.upcase(extension) <> "Handler")
if Code.ensure_loaded?(module) do
module
else
raise "Could not find handler for #{extension}"
end
end

defp upcase(<<h, t :: binary>>) when h in ?a..?z do
<<h - 32, upcase(t) :: binary>>
end

defp upcase(<<h, t :: binary>>) do
<<h, upcase(t) :: binary>>
end

defp upcase(<<>>) do
<<>>
end
end

defmodule Dynamo.View.EEXHandler do
@moduledoc false
@behaviour Dynamo.View.Handler

def compile(Dynamo.View.Template[source: source, identifier: identifier], locals) do
locals = [:assigns|locals]
match = match(locals)
vars = vars(locals)
args = [{ :assigns, 0, nil }|vars]
match = match(args)
source = EEx.compile_string(source, file: identifier)

{ args(locals), quote do
{ args, quote do
__block__ unquote(match)
unquote(source)
body = unquote(source)
{ unquote(vars), body }
end }
end

def render(module, function, locals, assigns) do
apply module, function, [assigns|Keyword.values(locals)]
end

defp args(locals) do
defp vars(locals) do
lc name inlist locals, do: { name, 0, nil }
end

defp match(locals) do
lc name inlist locals, do: { :=, 0, [{ :_, 0, nil }, { name, 0 , nil }] }
lc var inlist locals, do: { :=, 0, [{ :_, 0, nil }, var] }
end
end
6 changes: 6 additions & 0 deletions test/dynamo/router/rendering_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ defmodule Dynamo.Router.RenderingTest do
assert conn.resp_content_type == "text/html"
end

test "returns the updated connection" do
conn = get("/updatable.html")
assert conn.resp_body == "UPDATE\n\nCONN"
assert conn.assigns[:template] == :eex
end

test "works with layouts" do
conn = get("/with_layout")
assert conn.resp_body == "<html>\nHELLO!\n</html>"
Expand Down
1 change: 0 additions & 1 deletion test/dynamo/view/finder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ defmodule Dynamo.View.PathFinderTest do

test "returns all templates" do
all = @path_finder.all
assert length(all) == 3
assert Enum.find all, fn(t) -> t.key == "hello.html" end
assert Enum.find all, fn(t) -> t.key == "module.html" end
end
Expand Down
6 changes: 4 additions & 2 deletions test/dynamo/view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ defmodule Dynamo.ViewTest do
handler: Dynamo.View.EEXHandler, format: "html", ref: { CompileTest.CompiledViews, _ }] = template

{ mod, fun } = template.ref
assert apply(mod, fun, [[], nil]) == "HELLO!"
assert apply(mod, fun, [[], nil]) == { [nil], "HELLO!" }
end

defp render(query) do
Dynamo.View.render Dynamo.View.find(query, @view_paths), [conn: nil], []
{ [nil], body } =
Dynamo.View.render Dynamo.View.find(query, @view_paths), [conn: nil], []
body
end
end
3 changes: 3 additions & 0 deletions test/fixtures/views/updatable.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
UPDATE
<% conn = conn.assign(:template, :eex) %>
CONN

0 comments on commit 3f8f980

Please sign in to comment.