File size: 1,119 Bytes
936b397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
defmodule Plausible.CustomerSupport.Resource do
  @moduledoc """
  Generic behaviour for CS resources
  """
  defstruct [:id, :type, :module, :object, :path]

  @type schema() :: map()

  @type t() :: %__MODULE__{
          id: pos_integer(),
          module: atom(),
          object: schema(),
          type: String.t()
        }

  @callback search(String.t(), Keyword.t()) :: list(schema())
  @callback get(pos_integer()) :: schema()
  @callback path(any()) :: String.t()
  @callback dump(schema()) :: t()

  defmacro __using__(type: type) do
    quote do
      @behaviour Plausible.CustomerSupport.Resource
      alias Plausible.CustomerSupport.Resource
      alias PlausibleWeb.Router.Helpers, as: Routes

      import Ecto.Query
      alias Plausible.Repo

      @impl true
      def dump(schema) do
        new(__MODULE__, schema)
      end

      defoverridable dump: 1

      def new(module, schema) do
        %Resource{
          id: schema.id,
          type: unquote(type),
          path: module.path(schema.id),
          module: module,
          object: schema
        }
      end
    end
  end
end