Skip to content

Commit

Permalink
Add helper function and change matcher so it converts keys to atoms o…
Browse files Browse the repository at this point in the history
…nly if they already exist
  • Loading branch information
Peter Arentsen committed Jan 30, 2017
1 parent 224007f commit 3c4ab5f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
10 changes: 10 additions & 0 deletions lib/liquid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ defmodule Liquid do
end
def even_elements([]), do: []
end

defmodule Atomizer do
def to_existing_atom(string) do
try do
String.to_existing_atom(string)
rescue
ArgumentError -> nil
end
end
end
end
6 changes: 3 additions & 3 deletions lib/protocols/matcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ defimpl Liquid.Matcher, for: Liquid.Context do
current = cond do
current.assigns |> Map.has_key?(key) -> current.assigns
current.presets |> Map.has_key?(key) -> current.presets
!(is_nil(Map.get(current.assigns, key |> String.to_atom))) -> current.assigns
!(is_nil(Map.get(current.presets, key |> String.to_atom))) -> current.presets
!(is_nil(Map.get(current.assigns, key |> Liquid.Atomizer.to_existing_atom))) -> current.assigns
!(is_nil(Map.get(current.presets, key |> Liquid.Atomizer.to_existing_atom))) -> current.presets
is_map(current.assigns) and Map.has_key?(current.assigns, :__struct__) -> current.assigns
true -> nil
end
Expand Down Expand Up @@ -73,7 +73,7 @@ defimpl Liquid.Matcher, for: Any do
end

def match(current, key) when is_map(current) and is_binary(key) do
key = if Map.has_key?(current, :__struct__), do: key |> String.to_atom, else: key
key = if Map.has_key?(current, :__struct__), do: key |> Liquid.Atomizer.to_existing_atom, else: key
current |> Map.get(key)
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Liquid.Mixfile do

def project do
[ app: :liquid,
version: "0.4.0",
version: "0.5.0",
elixir: "~> 1.3",
deps: deps,
name: "Liquid",
Expand Down

0 comments on commit 3c4ab5f

Please sign in to comment.