From 5c9621e57e6a6901618bfd687534dbac7b6047fb Mon Sep 17 00:00:00 2001 From: Daniel Berkompas Date: Fri, 17 Apr 2015 15:27:36 -0700 Subject: [PATCH] Fix #3: String Interpolation --- lib/ex_twiml.ex | 33 ++++++++++++++++++++------------- test/ex_twiml_test.exs | 14 +++++++++++++- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/lib/ex_twiml.ex b/lib/ex_twiml.ex index f3db235..7b8881f 100644 --- a/lib/ex_twiml.ex +++ b/lib/ex_twiml.ex @@ -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. """ diff --git a/test/ex_twiml_test.exs b/test/ex_twiml_test.exs index 794344a..cb5ae53 100644 --- a/test/ex_twiml_test.exs +++ b/test/ex_twiml_test.exs @@ -1,5 +1,5 @@ defmodule ExTwimlTest do - use ExUnit.Case + use ExUnit.Case, async: false import ExTwiml test "Can render the verb" do @@ -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, "Hello, Daniel!Hello, Hunter!" + end + defp assert_twiml(lhs, rhs) do assert lhs == "#{rhs}" end