Skip to content

Commit

Permalink
Merge pull request #172 from up2jj/master
Browse files Browse the repository at this point in the history
Optimize checking gates
  • Loading branch information
tompave authored Aug 7, 2024
2 parents 064dda3 + b18c505 commit 3ba4763
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions lib/fun_with_flags/flag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,26 @@ defmodule FunWithFlags.Flag do

defp check_actor_gates(gates, item) do
gates
|> actor_gates()
|> do_check_actor_gates(item)
end


defp do_check_actor_gates([], _), do: :ignore

defp do_check_actor_gates([gate|rest], item) do
defp do_check_actor_gates([%Gate{type: :actor} = gate | rest], item) do
case Gate.enabled?(gate, for: item) do
:ignore -> do_check_actor_gates(rest, item)
result -> result
end
end

defp do_check_actor_gates([_gate | rest], item) do
do_check_actor_gates(rest, item)
end


defp check_group_gates(gates, item) do
gates
|> group_gates()
|> do_check_group_gates(item)
end

Expand All @@ -92,14 +95,18 @@ defmodule FunWithFlags.Flag do

defp do_check_group_gates([], _, result), do: result

defp do_check_group_gates([gate|rest], item, temp_result) do
defp do_check_group_gates([%Gate{type: :group} = gate|rest], item, temp_result) do
case Gate.enabled?(gate, for: item) do
:ignore -> do_check_group_gates(rest, item, temp_result)
{:ok, false} -> {:ok, false}
{:ok, true} -> do_check_group_gates(rest, item, {:ok, true})
end
end

defp do_check_group_gates([_gate | rest], item, temp_result) do
do_check_group_gates(rest, item, temp_result)
end


defp check_boolean_gate(gates) do
gate = boolean_gate(gates)
Expand Down Expand Up @@ -133,14 +140,6 @@ defmodule FunWithFlags.Flag do
Enum.find(gates, &Gate.boolean?/1)
end

defp actor_gates(gates) do
Enum.filter(gates, &Gate.actor?/1)
end

defp group_gates(gates) do
Enum.filter(gates, &Gate.group?/1)
end

defp percentage_of_time_gate(gates) do
Enum.find(gates, &Gate.percentage_of_time?/1)
end
Expand Down

0 comments on commit 3ba4763

Please sign in to comment.