Created
May 10, 2019 01:12
-
-
Save costaraphael/260be02adb6e1f2b8b5831f47b43e214 to your computer and use it in GitHub Desktop.
Revisions
-
costaraphael created this gist
May 10, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ defmodule Person do defstruct [:name, :age] def with do {Person.Builder, %{}} end defmodule Builder do def unquote(:'$handle_undefined_function')(:and_with, [{__MODULE__, _acc} = tuple]) do tuple end def unquote(:'$handle_undefined_function')(attr, [value, {__MODULE__, acc}]) do {__MODULE__, Map.put(acc, attr, value)} end def build!({__MODULE__, attrs}) do struct!(Person, attrs) end end end person = Person .with.name("John") .and_with.age(35) .build!() IO.inspect(person) #=> %Person{age: 35, name: "John"}