Toggle is a small feature toggle library for Crystal.
- Feature Toggles (Martin Fowler)
- Using Feature Flags to Ship Changes with Confidence (Travis CI)
- Feature flags in the development of GitLab (Gitlab)
-
Add the dependency to your
shard.yml
:dependencies: toggle: github: kandayo/toggle.cr
-
Run
shards install
Use Toggle#init
to initialize a global, easy-to-use default instance.
require "toggle"
require "toggle/backend/redis"
redis = Redis::PooledClient.new
backend = Toggle::Backend::Redis.new(redis: redis, keyspace: "_app_toggles")
Toggle.init(backend: backend)
Toggle.enable("payment_methods::pix")
Toggle.enabled?("payment_methods::pix") # => true
You can also instantiate multiple instances. Useful for managing features in large applications.
require "toggle"
require "toggle/backend/redis"
redis = Redis::PooledClient.new
tenant1_backend = Toggle::Backend::Redis.new(redis: redis, keyspace: "_tenant1_toggles")
tenant1 = Toggle::Instance.new(backend: tenant1_backend)
tenant2_backend = Toggle::Backend::Redis.new(redis: redis, keyspace: "_tenant2_toggles")
tenant2 = Toggle::Instance.new(backend: tenant2_backend)
# Enable "store::async_checkout" only for Tenant 1.
tenant1.enable("store::new_checkout")
tenant1.enabled?("store::new_checkout") # => true
tenant2.enabled?("store::new_checkout") # => false
# Or:
Toggle.enabled?(tenant1, "store::new_checkout") # => true
Toggle.enabled?(tenant2, "store::new_checkout") # => false
require "redis"
require "toggle"
require "toggle/backend/redis"
backend = Toggle::Backend::Redis.new(redis: redis, keyspace: "_app_toggles")
Features are stored in a HASH on Redis.
require "toggle"
require "toggle/backend/memory"
backend = Toggle::Backend::Memory.new
Features are stored in memory.
You can create your own backend using the Toggle::Backend::Base
interface.
- Fork it (https://github.com/kandayo/toggle.cr/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
- kandayo - creator and maintainer
The lib is available as open source under the terms of the MIT License.