aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlthms <contact@thomasletan.fr>2017-04-29 17:46:25 +0000
committerThomas Letan <contact@thomasletan.fr>2018-01-24 08:09:30 +0100
commit343b3497939870e5c8bba0ac019bdd7a2a18e4b5 (patch)
tree60845309d4b9b733da2fe780d2dc75fadc0f5365
parentchore: Generalize the specs parsing (diff)
entity: Simplify the implementation of both Puppet and Map
-rw-r--r--lib/lkn/core/map.ex24
-rw-r--r--lib/lkn/core/puppet.ex24
2 files changed, 14 insertions, 34 deletions
diff --git a/lib/lkn/core/map.ex b/lib/lkn/core/map.ex
index 61b497f..3c60923 100644
--- a/lib/lkn/core/map.ex
+++ b/lib/lkn/core/map.ex
@@ -4,37 +4,27 @@ defmodule Lkn.Core.Map do
"""
@type k :: any
- defp parse_def(lines, comps, legit) do
- case lines do
- [{:@, _, [{:components, _, [cps]}]}
- |rest] ->
- parse_def rest, cps, legit
- [x|rest] ->
- parse_def rest, comps, [x|legit]
- [] ->
- {comps, Enum.reverse(legit)}
- end
- end
-
defmacro defmap(name, do: block) do
lines = case block do
{:__block__, _, x} -> x
x -> [x]
end
- {comps, legit} = parse_def(lines, [], [])
-
quote do
defmodule unquote(name) do
@after_compile __MODULE__
- use Lkn.Core.Entity, components: unquote(comps)
+ unquote(lines)
+
+ def components do
+ @components
+ end
- unquote(legit)
+ use Lkn.Core.Entity, components: @components
def __after_compile__(_env, _bytecode) do
# check if the components are effectively valid
- Enum.map(unquote(comps), fn cx ->
+ Enum.map(components(), fn cx ->
c = Macro.expand(cx, __MODULE__)
gold = c.specs().system().component(:map)
diff --git a/lib/lkn/core/puppet.ex b/lib/lkn/core/puppet.ex
index 8739b7a..3ddd8b5 100644
--- a/lib/lkn/core/puppet.ex
+++ b/lib/lkn/core/puppet.ex
@@ -3,37 +3,27 @@ defmodule Lkn.Core.Puppet do
A key to identify and reach a Puppet.
"""
@type k :: any
- defp parse_def(lines, comps, legit) do
- case lines do
- [{:@, _, [{:components, _, [cps]}]}
- |rest] ->
- parse_def rest, cps, legit
- [x|rest] ->
- parse_def rest, comps, [x|legit]
- [] ->
- {comps, Enum.reverse(legit)}
- end
- end
-
defmacro defpuppet(name, do: block) do
lines = case block do
{:__block__, _, x} -> x
x -> [x]
end
- {comps, legit} = parse_def(lines, [], [])
-
quote do
defmodule unquote(name) do
@after_compile __MODULE__
- use Lkn.Core.Entity, components: unquote(comps)
+ unquote(lines)
+
+ def components do
+ @components
+ end
- unquote(legit)
+ use Lkn.Core.Entity, components: @components
def __after_compile__(_env, _bytecode) do
# check if the components are effectively valid
- Enum.map(unquote(comps), fn cx ->
+ Enum.map(components(), fn cx ->
c = Macro.expand(cx, __MODULE__)
gold = c.specs().system().component(:puppet)