Last active
January 8, 2016 02:38
-
-
Save TobiG77/e8c041c69526c1c24bcb to your computer and use it in GitHub Desktop.
collection of persistence helper methods
This file contains hidden or 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
defmodule FunctionHead do | |
def create(attrs) when is_map(attrs) do | |
with {:ok, doc} <- new(attrs), | |
{:ok, _ } <- persist(doc), | |
do: {:ok, doc} | |
end | |
def persist(doc), do: ExAws.Dynamo.put_item(__MODULE__.table_name, doc) | |
def where(filter) when is_map(filter) do | |
with {:ok, response } <- ExAws.Dynamo.get_item(__MODULE__.table_name, filter), | |
decoded = decode(response), | |
do: new(decoded) | |
end | |
def new(attrs) | |
def new(attrs: nil), do: {:ok, nil} | |
def new(attrs) when is_map(attrs) do | |
with doc <- doc_struct(attrs), | |
IO.puts("doc: #{ inspect(doc) }"), | |
{:ok, mdoc} <- migrate(doc), | |
do: {:ok, doc_struct(mdoc)} | |
end | |
defp doc_struct(doc) | |
defp doc_struct(doc: nil), do: nil | |
defp doc_struct(doc) when is_map(doc), do: struct(__MODULE__, doc) | |
defp decode(doc) when is_map(doc) do | |
if Map.has_key?(doc, "Item"), do: (ExAws.Dynamo.Decoder.decode(doc["Item"], as: __MODULE__)) | |
end | |
def migrate(doc), do: doc | |
end | |
[dev@localhost function_head]$ iex -S mix | |
Erlang/OTP 18 [erts-7.2] [source-e6dd627] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] | |
lib/function_head.ex:18: warning: this clause cannot match because a previous clause at line 18 always matches | |
Compiled lib/function_head.ex | |
Interactive Elixir (1.2.0) - press Ctrl+C to exit (type h() ENTER for help) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment