Skip to content

Commit

Permalink
Merge pull request #3 from danielberkompas/3-interpolation
Browse files Browse the repository at this point in the history
Error when interpolating a map property
  • Loading branch information
danielberkompas committed Apr 17, 2015
2 parents 74ffe85 + 5c9621e commit cd28d6c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
33 changes: 20 additions & 13 deletions lib/ex_twiml.ex
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,31 @@ defmodule ExTwiml do
"""
defmacro unquote(verb)(string \\ [], options \\ [])
defmacro unquote(verb)(string_or_options, options) do
current_verb = unquote(verb)

{expanded, _} = Code.eval_quoted(string_or_options, Map.get(__CALLER__, :vars), __ENV__)
case string_or_options do
string when is_binary(string) ->
compile_string_macro(unquote(verb), options, string)
{:<<>>, _, _} ->
compile_string_macro(unquote(verb), options, string_or_options)
_ ->
compile_nested_macro(unquote(verb), string_or_options)
end
end
end

if is_binary(expanded) do
quote do
tag unquote(current_verb), unquote(options) do
text unquote(string_or_options)
end
end
else
quote do
put_buffer var!(buffer, Twiml), opening_tag(unquote(current_verb), " /", unquote(string_or_options))
end
defp compile_string_macro(verb, options, string) do
quote do
tag unquote(verb), unquote(options) do
text unquote(string)
end
end
end

defp compile_nested_macro(verb, options) do
quote do
put_buffer var!(buffer, Twiml), opening_tag(unquote(verb), " /", unquote(options))
end
end

@doc """
Add an option to the output.
"""
Expand Down
14 changes: 13 additions & 1 deletion test/ex_twiml_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule ExTwimlTest do
use ExUnit.Case
use ExUnit.Case, async: false
import ExTwiml

test "Can render the <Gather> verb" do
Expand Down Expand Up @@ -192,6 +192,18 @@ defmodule ExTwimlTest do
assert opts == [{1, []}, {2, []}, {3, []}]
end

test ".twiml can loop through lists of maps" do
people = [%{name: "Daniel"}, %{name: "Hunter"}]

xml = twiml do
Enum.each people, fn person ->
say "Hello, #{person.name}!"
end
end

assert_twiml xml, "<Say>Hello, Daniel!</Say><Say>Hello, Hunter!</Say>"
end

defp assert_twiml(lhs, rhs) do
assert lhs == "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response>#{rhs}</Response>"
end
Expand Down

0 comments on commit cd28d6c

Please sign in to comment.