Skip to content

Commit

Permalink
feat: section option configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Jun 27, 2020
1 parent 8c1886d commit 564d095
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/ash/data_layer/ets/ets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule Ash.DataLayer.Ets do

@spec private?(Ash.resource()) :: boolean
def private?(resource) do
Extension.get_opt(resource, [:ets], :private?) || false
Extension.get_opt(resource, [:ets], :private?, false, true)
end

defmodule Query do
Expand Down
48 changes: 45 additions & 3 deletions lib/ash/dsl/extension.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,51 @@ defmodule Ash.Dsl.Extension do
:persistent_term.get({resource, :ash, path}, %{entities: []}).entities
end

@doc "Get an option value for a section at a given path"
def get_opt(resource, path, value) do
:persistent_term.get({resource, :ash, path}, %{opts: []}).opts[value]
@doc """
Get an option value for a section at a given path.
Checks to see if it has been overridden via configuration.
"""
def get_opt(resource, path, value, default, configurable? \\ false) do
if configurable? do
case get_opt_config(resource, path, value) do
{:ok, value} ->
value

_ ->
Keyword.get(
:persistent_term.get({resource, :ash, path}, %{opts: []}).opts,
value,
default
)
end
else
Keyword.get(
:persistent_term.get({resource, :ash, path}, %{opts: []}).opts,
value,
default
)
end
end

def get_opt_config(resource, path, value) do
with {:ok, config} <- Application.fetch_env(:ash, resource),
{:ok, value} <-
path
|> List.wrap()
|> Kernel.++([value])
|> Enum.reduce_while({:ok, config}, fn key, {:ok, config} ->
if Keyword.keyword?(config) do
case Keyword.fetch(config, key) do
{:ok, value} -> {:cont, {:ok, value}}
:error -> {:halt, :error}
end
else
{:halt, :error}
end
end) do
{:ok, value}
end
end

@doc false
Expand Down
11 changes: 10 additions & 1 deletion lib/ash/dsl/section.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ defmodule Ash.Dsl.Section do
For a full example, see `Ash.Dsl.Extension`.
"""
defstruct [:name, imports: [], schema: [], describe: "", entities: [], sections: []]
defstruct [
:name,
imports: [],
schema: [],
configurable: [],
describe: "",
entities: [],
sections: []
]

@type t :: %__MODULE__{
name: atom,
describe: String.t(),
entities: [Ash.Dsl.Entity.t()],
sections: [%__MODULE__{}],
configurable: [atom],
schema: NimbleOptions.schema()
}

Expand Down

0 comments on commit 564d095

Please sign in to comment.