toggle.cr v0.1.1

Feature toggles for Crystal

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

# Or:

Toggle.enabled?(tenant1, "store::new_checkout") # => true
Toggle.enabled?(tenant2, "store::new_checkout") # => false

Backend

Redis Backend

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.

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/toggle.cr/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.

Repository

toggle.cr

Owner
Statistic
  • 2
  • 0
  • 0
  • 0
  • 2
  • over 2 years ago
  • August 26, 2021
License

MIT License

Links
Synced at

Mon, 06 May 2024 23:31:49 GMT

Languages