Skip to content

Instantly share code, notes, and snippets.

@Sleepful
Last active November 18, 2022 21:45
Show Gist options
  • Save Sleepful/3542b75831dd8ee9d9dcd05444759d27 to your computer and use it in GitHub Desktop.
Save Sleepful/3542b75831dd8ee9d9dcd05444759d27 to your computer and use it in GitHub Desktop.
defmodule CaseCondIf do
defp set_required(set) do
if !set.valid? do
set
else
state = get_field(set, :__meta__).state
IO.inspect(state)
stage = set.stage
# set_required(set, state, stage)
case {stage, state} do
{:new, :built} ->
set
{:submit, :built} ->
type = get_field(set, :type)
assoc = get_field(set, type)
(assoc && put_assoc(set, type, required: assoc.required)) || set
{:new, :loaded} ->
type = get_field(set, :type)
assoc = get_field(set, type)
name =
case assoc.required do
true -> "Yes"
false -> "No"
nil -> "?"
end
schema =
set.data
|> Map.put(:required, assoc.required)
|> Map.put(:required_name, name)
put_in(set.data, schema)
{:submit, :loaded} ->
type = get_field(set, :type)
required = get_field(set, :required)
assoc = get_field(set, type, nil)
assoc_change = change(assoc, %{required: required})
put_change(set, type, assoc_change)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment