Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danielefongo/features
Enable or disable code using feature toggles
https://github.com/danielefongo/features
Last synced: 7 days ago
JSON representation
Enable or disable code using feature toggles
- Host: GitHub
- URL: https://github.com/danielefongo/features
- Owner: danielefongo
- License: agpl-3.0
- Created: 2021-10-26T13:18:36.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-11T06:58:39.000Z (about 3 years ago)
- Last Synced: 2024-10-04T00:35:03.903Z (about 1 month ago)
- Language: Elixir
- Homepage:
- Size: 53.7 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Features
Features is an Elixir porting of [rust cargo features](https://doc.rust-lang.org/cargo/reference/features.html).
## Disclaimer
**This package is experimental!**
Unexpected behavior may happen, so please try this library and [open an issue](https://github.com/danielefongo/features/issues/new) if you find a bug 💜.
## Installation
Add `features` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:features, "~> 0.1.0"}
]
end
```## Usage
On `config.exs`:
```elixir
config :features, features: [:a_feature]
```On code:
```elixir
defmodule MyModule do
use Features@doc "A doc for do_something/0 function."
# this will enable the next function with his doc if :a_feature is in config
@feature :a_feature
def do_something do
:a_feature_is_enabled# this will enable the next statement if :another_feature is in config
@feature :another_feature
:another_feature_is_enabled# this will enable the next statement if :another_feature is not in config
@feature_off :another_feature
:another_feature_is_disabled
end
end
```Code is automatically removed during compilation if the feature condition is not met.
A config example is the following:
```elixir
config :features, features: [:a_feature]
```
### TestingTo test featured code you have to set features property to enable runtime execution (it replaces the compile-time deletion).
On `test.exs`:
```elixir
config :features, test: true
```On `test_helpers.exs`:
```elixir
Features.Test.start()
```On a test:
```elixir
defmodule MyModuleTest do
use ExUnit.Case, async: true
use Features.Testfeatured_test "test1", features: [:a_feature, :another_feature] do
assert MyModule.do_something() == :another_feature_is_enabled
endfeatured_test "test2", features: [:a_feature] do
assert MyModule.do_something() == :another_feature_is_disabled
endfeatured_test "test3", features: [] do
assert_raise CaseClauseError, fn -> MyModule.do_something() end
end
end
```