-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Simplify dependencies - Remove structs (use maps) - Make use of helper functions more
- Loading branch information
Showing
55 changed files
with
1,317 additions
and
1,535 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule YahooFantasyEx.Model do | ||
@moduledoc false | ||
@spec __using__(Keyword.t()) :: Macro.t() | ||
defmacro __using__(opts) do | ||
quote do | ||
import YahooFantasyEx.Models.Helpers | ||
alias __MODULE__ | ||
alias YahooFantasyEx.Models.Decoder | ||
|
||
@fields unquote(opts[:fields]) | ||
|
||
@type t :: %{} | ||
|
||
@spec new(any()) :: t() | ||
def new(json), do: Decoder.decode(json, @fields) | ||
|
||
@spec new(any(), map()) :: t() | ||
def new(decoded, parent), do: Decoder.decode_subresources(decoded, parent, @fields) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
defmodule YahooFantasyEx.Models.Decoder do | ||
@moduledoc false | ||
|
||
alias YahooFantasyEx.Models.Decoder.Function | ||
alias YahooFantasyEx.Models.Decoder.Primitive | ||
alias YahooFantasyEx.Models.Decoder.Subresource | ||
|
||
@spec decode(any(), any()) :: {:error, Jason.DecodeError.t()} | map() | ||
def decode(decoded, fields) do | ||
flattened = flatten_attributes(decoded) | ||
Map.new(fields, &parse(&1, flattened)) | ||
end | ||
|
||
def decode_subresources(decoded, parent, fields) do | ||
flattened = flatten_attributes(decoded) | ||
Map.new(fields, &parse(&1, flattened, parent)) | ||
end | ||
|
||
defp parse({field, type_or_func}, entity, parent \\ nil) do | ||
value = Map.get(entity, field) | ||
{field, do_parse(type_or_func, value, entity, parent)} | ||
end | ||
|
||
defp do_parse(function, value, decoded, parent) when is_function(function), | ||
do: Function.apply(function, value, decoded, parent) | ||
|
||
defp do_parse({:array, function}, value, decoded, parent) when is_function(function), | ||
do: Function.apply_many(function, value, decoded, parent) | ||
|
||
defp do_parse({:many, module}, value, decoded, _parent), | ||
do: Subresource.build(value, module, decoded) | ||
|
||
defp do_parse({:array, type}, value, _decoded, _parent), do: Primitive.cast_many(type, value) | ||
defp do_parse(type, value, _decoded, _parent), do: Primitive.cast(type, value) | ||
|
||
defp flatten_attributes(attributes) do | ||
attributes | ||
|> List.flatten() | ||
|> Enum.reduce(&Map.merge(&1, &2, fn _k, v1, v2 -> v2 ++ v1 end)) | ||
end | ||
|
||
defmodule Primitive do | ||
@moduledoc false | ||
alias YahooFantasyEx.Models.Helpers | ||
|
||
def cast(:atom, value), do: Helpers.cast_atom!(value) | ||
def cast(:boolean, value), do: Helpers.cast_boolean(value) | ||
def cast(:date, value), do: Helpers.cast_date(value) | ||
def cast(:float, value), do: Helpers.cast_float(value) | ||
def cast(:integer, value), do: Helpers.cast_integer(value) | ||
def cast(:string, value), do: value | ||
|
||
def cast_many(type, values), do: Enum.map(values, &cast(type, &1)) | ||
end | ||
|
||
defmodule Function do | ||
@moduledoc false | ||
def apply(function, value, _decoded, _parent) when is_function(function, 1), | ||
do: function.(value) | ||
|
||
def apply(function, value, decoded, _parent) when is_function(function, 2), | ||
do: function.(value, decoded) | ||
|
||
def apply(function, value, decoded, parent) when is_function(function, 3), | ||
do: function.(value, decoded, parent) | ||
|
||
def apply_many(function, values, decoded, parent), | ||
do: Enum.map(values, &apply(function, &1, decoded, parent)) | ||
end | ||
|
||
defmodule Subresource do | ||
@moduledoc false | ||
def build(value, module, decoded) do | ||
value | ||
|> Enum.map(&flatten/1) | ||
|> Enum.map(&module.new(&1, decoded)) | ||
end | ||
|
||
defp flatten({_, data}) do | ||
data | ||
|> Map.values() | ||
|> List.flatten() | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.