0.0
No commit activity in last 3 years
No release in over 3 years
Fast, safe configuration for Ruby on Rails applications.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.16
~> 0.16
~> 0.12
~> 10.0
~> 3.0
~> 1.3
~> 0.9

Runtime

>= 4.0.0
 Project Readme

CaptainConfig

Build Status

CaptainConfig makes it easy to add developer-friendly configuration to control your application's behavior.

Getting Started

Add the gem to your application's Gemfile:

gem 'captain_config'

Run the generator to create the migration for the captain_configs table:

bundle exec rails generate captain_config

Not on Rails? Check out the template for the ActiveRecord migration that would have been generated; that describes how this gem expects that table to look.

Then—assuming you're using Rails—set up some configuration in your config/initializers:

# config/initializers/captain_config.rb
CONFIG = CaptainConfig::Service.new do
  new_feature_enabled :boolean, default: false
  important_user_points_threshold :integer, default: 9000
end

Add the Puma middleware so that the configuration is reloaded out-of-band in between requests:

# config.ru
require_relative 'config/environment'

use CaptainConfig::PumaMiddleware

run Rails.application

Now you can read the configuration anywhere! The configuration will be reloaded in between requests, so reading it in your application is a fast hash lookup.

class User < Application
  def met_threshold?
    points >= CONFIG[:important_user_points_threshold]
  end
end
<% if CONFIG[:new_feature_enabled] %>
  <%= link_to 'New Stuff!', new_feature_path %>
<% end >

Sidekiq

There is a Sidekiq middleware to automatically reload configuration.

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add CaptainConfig::SidekiqMiddlewareFactory.build

    # By default it only reloads every 1 second. Use the `interval:` argument
    # to change this frequency:
    #
    # chain.add CaptainConfig::SidekiqMiddlewareFactory.build(interval: 5.0)
  end
end

Contributing

Clone the repo and run bundle install to get started developing locally.

There are integration specs which set up a sample Rails application and run tests against it. As that takes some time (more than a few seconds), there is a script to run just the non-integration specs:

./script/rspec-without-integration

Arguments are forwarded to RSpec, so if you want to run an individual spec file:

./script/rspec-without-integration spec/captain_config_spec.rb

Bug reports and pull requests are welcome on the GitHub repository.

Code of Conduct

This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct. Everyone interacting in the project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

License

Released under the Modified BSD license, see LICENSE for details.