diff --git a/lib/cereal/builders/entity.ex b/lib/cereal/builders/entity.ex index 20219e0..b2e1737 100644 --- a/lib/cereal/builders/entity.ex +++ b/lib/cereal/builders/entity.ex @@ -21,6 +21,8 @@ defmodule Cereal.Builders.Entity do defp attributes(%{serializer: serializer} = context) do serializer.attributes(context.data, context.conn) + |> Enum.map(fn {key, fnc} -> {key, fnc.(context.data, context.conn)} end) + |> Enum.into(%{}) |> filter_attributes(context) end @@ -48,7 +50,7 @@ defmodule Cereal.Builders.Entity do defp build_relation_entity(nil, _, _, _), do: nil defp build_relation_entity(relation, context, name, rel_opts) do - context + context |> Map.put(:serializer, rel_opts.serializer) |> Map.put(:opts, with_relationship_includes(context.opts, name)) |> Map.put(:data, relation) diff --git a/lib/cereal/serializer.ex b/lib/cereal/serializer.ex index ffdae8f..dfa4b70 100644 --- a/lib/cereal/serializer.ex +++ b/lib/cereal/serializer.ex @@ -70,7 +70,7 @@ defmodule Cereal.Serializer do quote do def attributes(struct, conn) do __MODULE__.__attributes() - |> Enum.map(&({&1, apply(__MODULE__, &1, [struct, conn])})) + |> Enum.map(& {&1, fn struct, conn -> apply(__MODULE__, &1, [struct, conn]) end}) |> Enum.into(%{}) end defoverridable [attributes: 2] @@ -100,18 +100,7 @@ defmodule Cereal.Serializer do @attributes @attributes ++ attrs for attr <- attrs do - has_function? = :erlang.function_exported(__MODULE__, attr, 2) - - def unquote(attr)(m, c) do - # For each attribute, see if there is a function exported on the - # module with a /1 arity, otherwise fallback to grabbing from the - # entity passed in. - unquote(has_function?) - |> case do - true -> apply(__MODULE__, unquote(attr), [m, c]) - false -> Map.get(m, unquote(attr)) - end - end + def unquote(attr)(m, _), do: Map.get(m, unquote(attr)) defoverridable [{attr, 2}] end end