toggle

Version, currently 0.1.0-pre2 versions

github.com/lucasintel/toggle.cr

Feature toggles for Crystal

2 stars
0 dependents
License: MIT

Nothing has been indexed for 0.1.0-pre yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.

Installation

# Add this to your shard.yml
dependencies:
  toggle:
    github: lucasintel/toggle.cr
    version: ~> 0.1.0-pre

Then run:

shards install

shard.yml

No shard.yml has been indexed for 0.1.0-pre. You can read it on the repository.

Dependencies

Unknown: the shard.yml for this version has not been read yet.

README

This README is the one indexed from the repository at its latest ref, not from the tag for this version.

Toggle

Built with Crystal 1.1.1 GitHub release CI

Toggle is a small feature toggle library for Crystal.

References

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      toggle:
        github: kandayo/toggle.cr
  2. Run shards install

Usage

Getting Started

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

Backend

Redis Backend

require "redis"

require "toggle"
require "toggle/backend/redis"

backend = Toggle::Backend::Redis.new(redis: redis, keyspace: "_tenant1_toggles")

Features are stored in a HASH on Redis.

In Memory Backend

require "toggle"
require "toggle/backend/memory"

backend = Toggle::Backend::Memory.new

Features are stored in memory.

Custom Backend

You can create your own backend using the Toggle::Backend::Base interface.

Contributing

  1. Fork it (https://github.com/kandayo/togglecr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

License

The lib is available as open source under the terms of the MIT License.