Packages
unleash_fresha
1.14.1-git-e754
5.1.4
5.1.3
5.1.2
5.1.1
5.1.0
5.0.0
4.0.0
4.0.0-git-bda3
3.0.1
3.0.0
2.1.0
2.0.1
2.0.0-git-75d8
2.0.0-git-67b2
2.0.0-git-4b61
2.0.0-git-49f4
1.16.0
1.15.0
1.14.1-git-e754
1.14.0
1.14.0-git-71e9
1.13.0
1.13.0-git-4ba6
1.12.0
1.11.0
1.11.0-git-1b62
1.10.2
1.10.2-git-b835
1.10.1
1.10.0
1.9.3-alpha0
1.9.2
1.9.1
1.9.0
An Unleash Feature Flag client for Elixir, forked from [unleash](https://gitlab.com/afontaine/unleash_ex)
Current section
Files
Jump to
Current section
Files
lib/unleash/feature.ex
defmodule Unleash.Feature do
@moduledoc false
alias Unleash.Strategy
alias Unleash.Variant
@derive Jason.Encoder
defstruct name: "",
description: "",
enabled: false,
strategies: [],
variants: %{}
def from_map(map) when is_map(map) do
%__MODULE__{
name: map["name"],
description: map["description"],
enabled: map["enabled"],
strategies: map["strategies"],
variants: Enum.map(map["variants"] || [], &Variant.from_map/1)
}
end
def from_map(_), do: %__MODULE__{}
def enabled?(nil, _context), do: {false, []}
def enabled?(%__MODULE__{enabled: enabled, strategies: []}, _context), do: {enabled, []}
def enabled?(%__MODULE__{enabled: enabled, strategies: strat}, context) when is_list(strat) do
# Attach namespace context from `ENVIRONMENT`, to allow staging deployments to work with a single Unleash environment
# This is a fresha-specific mechanism
context = Map.put_new(context, :namespace, System.get_env("UNLEASH_NAMESPACE"))
strategy_evaluations =
Enum.map(strat, fn strategy ->
{strategy["name"], Strategy.enabled?(strategy, context)}
end)
result =
strategy_evaluations
|> Enum.any?(fn {_, enabled?} -> enabled? end)
|> Kernel.and(enabled)
{result, strategy_evaluations}
end
end