-
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.
- Loading branch information
1 parent
a03e72a
commit 03b49a3
Showing
4 changed files
with
85 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Welcome to the home for [my personal livebooks](https://github.com/christhekeele/livebooks/tree/latest/livebooks): Elixir experiments, guides, and accompanying source code. | ||
|
||
Their history can be accessed [on GitHub](https://github.com/christhekeele/livebooks) by the clicking the "view source" | ||
`</>` icon next to the title of every page. Corrections, proposals, and discussions can be made in the GitHub [issues](https://github.com/christhekeele/livebooks/issues), [pull requests](https://github.com/christhekeele/livebooks/pulls), and [discussions](https://github.com/christhekeele/livebooks/discussions) pages respectively. | ||
`</>` icon next to the title of every page. Corrections, proposals, and discussions can be made in the GitHub [pull requests](https://github.com/christhekeele/livebooks/pulls), [issues](https://github.com/christhekeele/livebooks/issues), and [discussions](https://github.com/christhekeele/livebooks/discussions) pages respectively. |
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,55 @@ | ||
defmodule Livebook.Helpers do | ||
@moduledoc """ | ||
Common helpers for these livebooks. | ||
""" | ||
|
||
defmacro __using__(_opts \\ []) do | ||
quote do | ||
import unquote(__MODULE__), only: [defmodule: 3] | ||
unquote(__MODULE__).start_patching_modules() | ||
end | ||
end | ||
|
||
@doc """ | ||
Inspired by [this macro](https://github.com/brettbeatty/experiments_elixir/blob/master/module_patching.livemd). | ||
""" | ||
defmacro defmodule(alias, [v: version], do_block) do | ||
[do: block] = do_block | ||
module = Macro.expand(alias, __CALLER__) | ||
version = Macro.expand(version, __CALLER__) | ||
version_namespace = "v#{version}" |> String.to_atom() | ||
module_namespace = Module.concat([:"Elixir", module, version_namespace]) | ||
store_block(module, version, block) | ||
|
||
quote do | ||
defmodule unquote(module_namespace) do | ||
(unquote_splicing(patches_so_far(module, version))) | ||
end | ||
|
||
alias unquote(module_namespace), as: unquote(alias) | ||
end | ||
|> IO.inspect() | ||
end | ||
|
||
defp store_block(module, version, block) do | ||
:ets.insert(__MODULE__, {{module, version}, block}) | ||
end | ||
|
||
defp patches_so_far(module, version) do | ||
__MODULE__ | ||
|> :ets.select([{{{module, :"$1"}, :"$2"}, [{:"=<", :"$1", version}], [:"$2"]}]) | ||
|> Enum.flat_map(&unblock/1) | ||
end | ||
|
||
defp unblock(ast) | ||
defp unblock({:__block__, _meta, block}), do: block | ||
defp unblock(ast), do: [ast] | ||
|
||
def start_patching_modules do | ||
with :undefined <- :ets.whereis(__MODULE__) do | ||
__MODULE__ = :ets.new(__MODULE__, [:ordered_set, :named_table]) | ||
end | ||
|
||
:ok | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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