• Stars
    star
    600
  • Rank 74,640 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 16 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A super cool, simple, and feature rich configuration system for Ruby apps.

Configatron

Build Status Code Climate

Configatron makes configuring your applications and scripts incredibly easy. No longer is a there a need to use constants or global variables. Now you can use a simple and painless system to configure your life. And, because it's all Ruby, you can do any crazy thing you would like to!

One of the more important changes to V3 is that it now resembles more a Hash style interface. You can use [], fetch, each, etc... Actually the hash notation is a bit more robust since the dot notation won't work for a few property names (a few public methods from Configatron::Store itself).

Installation

Add this line to your application's Gemfile:

gem 'configatron'

And then execute:

$ bundle

Or install it yourself as:

$ gem install configatron --pre

Usage

Once installed you just need to require it:

require 'configatron'

Simple

configatron.email = '[email protected]'
configatron.database.url = "postgres://localhost/foo"

Now, anywhere in your code you can do the following:

configatron.email # => "[email protected]"
configatron.database.url # => "postgres://localhost/foo"

Voila! Simple as can be.

Now you're saying, what if I want to have a 'default' set of options, but then override them later, based on other information? Simple again. Let's use our above example. We've configured our database.url option to be postgres://localhost/foo. The problem with that is that is our production database url, not our development url. Fair enough, all you have to do is redeclare it:

configatron.database.url = "postgres://localhost/foo_development"

becomes:

configatron.email # => "[email protected]"
configatron.database.url # => "postgres://localhost/foo_development"

Notice how our other configuration parameters haven't changed? Cool, eh?

Hash/YAML

You can configure Configatron from a hash as well (this is particularly useful if you'd like to have configuration files):

configatron.configure_from_hash(email: {pop: {address: 'pop.example.com', port: 110}}, smtp: {address: 'smtp.example.com'})

configatron.email.pop.address # => 'pop.example.com'
configatron.email.pop.port # => 110
# and so on...

Method vs hash access

As a note, method (configatron.foo) access will be shadowed by public methods defined on the configatron object. (The configatron object descends from BasicObject and adds a few methods to resemble the Hash API and to play nice with puts, so it should have a pretty bare set of methods.)

If you need to use keys that are themselves method names, you can just use hash access (configatron['foo']).

Namespaces

The question that should be on your lips is what I need to have namespaced configuration parameters. It's easy! Configatron allows you to create namespaces.

configatron.website_url = "http://www.example.com"
configatron.email.pop.address = "pop.example.com"
configatron.email.pop.port = 110
configatron.email.smtp.address = "smtp.example.com"
configatron.email.smtp.port = 25

configatron.to_h # => {:website_url=>"http://www.example.com", :email=>{:pop=>{:address=>"pop.example.com", :port=>110}, :smtp=>{:address=>"smtp.example.com", :port=>25}}}

Configatron allows you to nest namespaces to your hearts content! Just keep going, it's that easy.

Of course you can update a single parameter n levels deep as well:

configatron.email.pop.address = "pop2.example.com"

configatron.email.pop.address # => "pop2.example.com"
configatron.email.smtp.address # => "smtp.example.com"

Configatron will also let you use a block to clean up your configuration. For example the following two ways of setting values are equivalent:

configatron.email.pop.address = "pop.example.com"
configatron.email.pop.port = 110

configatron.email.pop do |pop|
  pop.address = "pop.example.com"
  pop.port = 110
end

Temp Configurations

Sometimes in testing, or other situations, you want to temporarily change some settings. You can do this with the temp method (only available on the top-level configatron RootStore):

configatron.one = 1
configatron.letters.a = 'A'
configatron.letters.b = 'B'
configatron.temp do
  configatron.letters.b = 'bb'
  configatron.letters.c = 'c'
  configatron.one # => 1
  configatron.letters.a # => 'A'
  configatron.letters.b # => 'bb'
  configatron.letters.c # => 'c'
end
configatron.one # => 1
configatron.letters.a # => 'A'
configatron.letters.b # => 'B'
configatron.letters.c # => {}

Delayed and Dynamic Configurations

There are times when you want to refer to one configuration setting in another configuration setting. Let's look at a fairly contrived example:

configatron.memcached.servers = ['127.0.0.1:11211']
configatron.page_caching.servers = configatron.memcached.servers
configatron.object_caching.servers = configatron.memcached.servers

if Rails.env == 'production'
  configatron.memcached.servers = ['192.168.0.1:11211']
  configatron.page_caching.servers = configatron.memcached.servers
  configatron.object_caching.servers = configatron.memcached.servers
elsif Rails.env == 'staging'
  configatron.memcached.servers = ['192.168.0.2:11211']
  configatron.page_caching.servers = configatron.memcached.servers
  configatron.object_caching.servers = configatron.memcached.servers
end

Now, we could've written that slightly differently, but it helps to illustrate the point. With Configatron you can create Delayed and Dynamic settings.

Delayed

With Delayed settings execution of the setting doesn't happen until the first time it is executed.

configatron.memcached.servers = ['127.0.0.1:11211']
configatron.page_caching.servers = Configatron::Delayed.new {configatron.memcached.servers}
configatron.object_caching.servers = Configatron::Delayed.new {configatron.memcached.servers}

if Rails.env == 'production'
  configatron.memcached.servers = ['192.168.0.1:11211']
elsif Rails.env == 'staging'
  configatron.memcached.servers = ['192.168.0.2:11211']
end

Execution occurs once and after that the result of that execution is returned. So in our case the first time someone calls the setting configatron.page_caching.servers it will find the configatron.memcached.servers setting and return that. After that point if the configatron.memcached.servers setting is changed, the original settings are returned by configatron.page_caching.servers.

Dynamic

Dynamic settings are very similar to Delayed settings, but with one big difference. Every time you call a Dynamic setting is executed. Take this example:

configatron.current.time = Configatron::Dynamic.new {Time.now}

Each time you call configatron.current.time it will return a new value to you. While this seems a bit useless, it is pretty useful if you have ever changing configurations.

Reseting Configurations

In some testing scenarios, it can be helpful to restore Configatron to its default state. This can be done with:

configatron.reset!

Checking keys

Even if parameters haven't been set, you can still call them, but you'll get a Configatron::Store object back. You can use .has_key? to determine if a key already exists.

configatron.i.dont.has_key?(:exist) # => false

(key)!

You can also append a ! to the end of any key. If the key exists it will return it, otherwise it will raise a Configatron::UndefinedKeyError.

configatron.a.b = 'B'
configatron.a.b # => 'B'
configatron.a.b! # => 'B'
configatron.a.b.c! # => raise Configratron::UndefinedKeyError

Kernel

The configatron "helper" method is stored in the Kernel module. You can opt-out of this global monkey-patching by requiring configatron/core rather than configatron. You'll have to set up your own Configatron::RootStore object.

Example:

require 'configatron/core'

store = Configatron::RootStore.new
store.foo = 'FOO'

store.to_h #= {foo: 'FOO'}

Locking

Once you have setup all of your configurations you can call the lock! method to lock your settings and raise an error should anyone try to change settings or access an unset setting later.

Example:

configatron.foo = 'FOO'
configatron.lock!

configatron.foo # => 'FOO'

configatron.bar # => raises Configatron::UndefinedKeyError
configatron.bar = 'BAR' # => raises Configatron::LockedError

Rails

Configatron works great with Rails. Use the built-in generate to generate an initializer file and a series of environment files for you to use to configure your applications.

$ rails generate configatron:install

Configatron will read in the config/configatron/defaults.rb file first and then the environment specific file, such as config/configatron/development.rb. Settings in the environment file will merge into and replace the settings in the defaults.rb file.

Example

# config/configatron/defaults.rb
configatron.letters.a = 'A'
configatron.letters.b = 'B'
# config/configatron/development.rb
configatron.letters.b = 'BB'
configatron.letters.c = 'C'
configatron.to_h # => {:letters=>{:a=>"A", :b=>"BB", :c=>"C"}}

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Write Tests!
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new Pull Request

Contributors

  • Mark Bates
  • Greg Brockman
  • Kurtis Rainbolt-Greene
  • Rob Sanheim
  • JΓ©rΓ©my Lecour
  • Cody Maggard
  • Jean-Denis Vauguet
  • chatgris
  • Simon Menke
  • Mat Brown
  • Torsten SchΓΆnebaum
  • Gleb Pomykalov
  • Casper Gripenberg
  • Artiom Diomin
  • mattelacchiato
  • Dan Pickett
  • Tim Riley
  • Rick Fletcher
  • Jose Antonio Pio
  • Brandon Dimcheff
  • joe miller
  • Josh Nichols

More Repositories

1

goth

Package goth provides a simple, clean, and idiomatic way to write authentication packages for Go web applications.
Go
5,512
star
2

pkger

Embed static files in Go binaries (replacement for gobuffalo/packr)
Go
1,185
star
3

grift

Go based task runner
Go
433
star
4

coffee-rails-source-maps

DO NOT USE THIS!! I DO NOT MAINTAIN IT!
Ruby
241
star
5

coffeebeans

The parts of CoffeeScript they forgot in Rails 3.1!
Ruby
228
star
6

cover_me

An RCov-esque coverage tool for Ruby 1.9
Ruby
204
star
7

refresh

Go
191
star
8

jquery-bootstrap-pagination

CoffeeScript
90
star
9

Programming-In-CoffeeScript

Source code for the Programming in CoffeeScript book.
CoffeeScript
63
star
10

going

Some helpful packages for writing Go apps.
Go
55
star
11

dj_remixes

Enhancements and improvements for Delayed Job 2.x
Ruby
42
star
12

mack

A Ruby web application framework
Ruby
41
star
13

warp_drive

Screw Rails Engines! Why not install a Warp Drive! Completely bundle up an ENTIRE Rails application into a gem, then load it into another application! It's that easy!
Ruby
34
star
14

myvim

My VIM setup. Use at your own risk.
Vim Script
34
star
15

buffla

URL Shortner written in Buffalo
Go
30
star
16

inflect

DEPRECATED: use github.com/gobuffalo/flect instead
Go
24
star
17

fluent-2014

Source code for the Angular Fundamentals workshop at FluentConf 2014.
JavaScript
22
star
18

ghi

Offline GitHub Issues Client
Go
19
star
19

cachetastic

A very simple, yet very powerful caching framework for Ruby
Ruby
18
star
20

delayed_job_extras

This project has moved to markbates/dj_remixes. Please do not follow this project!
Ruby
17
star
21

buffalo-heroku

Archived use github.com/gobuffalo/buffalo-heroku
Go
17
star
22

mongoid-tags-arent-hard

A tagging gem for Mongoid 3 that doesn't actually suck.
Ruby
16
star
23

distribunaut

A framework agnostic port of the mack-distributed package.
Ruby
16
star
24

yamler

Tools for using YAML w/ Ruby easier.
Ruby
11
star
25

gormrecipe

An example of using Gorm with Buffalo
Go
10
star
26

mack-more

All the extra stuff you could want for the Mack Framework.
Ruby
10
star
27

genosaurus

A simple and easy to use generator system for Ruby
Ruby
9
star
28

mongoid_delorean

THIS PROJECT IS NOT MAINTAINED. USE AT YOUR OWN RISK. NO PRs ACCEPTED.
Ruby
9
star
29

gocker

Go
9
star
30

informit_articles

InformIT Articles
Ruby
7
star
31

macker-s-guide

A User's Guide to the Mack Framework
JavaScript
7
star
32

gemstub

A simple gem for creating the stub of other gems
Ruby
7
star
33

api_doc

Easily generate API docs from RSpec/Rails
Ruby
7
star
34

faraday_bang

Adds error raising ! methods to Faraday
Ruby
6
star
35

wailsx

WIP: Tools for working with Wails.io
Go
6
star
36

gem-freezer

A simple gem that helps you freeze gems, and their dependencies into your project.
Ruby
5
star
37

contextual

context.Context utilities
Go
5
star
38

nor-dev

Development Playground for Node on Rails (NOR)
CoffeeScript
4
star
39

willie

Easy testing of Go web type stuff
Go
4
star
40

cash

A Go package for working with cash.Money :)
Go
4
star
41

oncer

Go
4
star
42

markdownr

Go
4
star
43

gobular

Go Regular Expression Tester
Go
4
star
44

breach

Ruby
3
star
45

gemtronics

A much better of managing your gem dependencies
Ruby
3
star
46

buffalo-bootstrap

A plugin for github.com/gobuffalo/buffalo for generating Bootstrap stuff
Go
3
star
47

braai

Fully Extensible Templates
Ruby
3
star
48

deplist

Get a list of all the Go dependencies for the current directory
Go
3
star
49

mack-user_auth_portlet

A collection of Portlets for the Mack Framework
Ruby
3
star
50

deano

A starter template for Sinatra Applications
Ruby
3
star
51

buffalo-rails

Go
3
star
52

sigtx

a context implementation for signal capturing
Go
3
star
53

micro-mack

A 'micro' version of the Mack Framework
Ruby
3
star
54

miki

A lightweight, Mack-based Wiki powering www.mackwiki.com
Ruby
3
star
55

jim

Go
3
star
56

mocha-coffeescript-tmbundle

TextMate/Sublime Text 2 bundle for Mocha (CoffeeScript)
2
star
57

errx

Makefile
2
star
58

coke

Go
2
star
59

bluffalo

Go
2
star
60

assoc

Go
2
star
61

chalk

Quickly and easily create a pool of `go routines`.
Go
2
star
62

control

Go
2
star
63

rails_templates

Templates for building Rails applications
Ruby
2
star
64

ekar

Ekar, a Rake replacement, maybe?
Ruby
2
star
65

rack-cachely

Rack Middleware for working with the CachelyApp Page Cache Service
Ruby
2
star
66

mark_facets

Just a collection of common extensions and add-ons I use in most Ruby/Rails projects.
Ruby
2
star
67

snooze_force

SnoozeForce - A Client for Saleforce.com's REST API
Ruby
2
star
68

syncx

Go
2
star
69

opal-rb-example

A "todo" example using the Opal.rb Project.
Ruby
2
star
70

blabber_mouth

Easy model-like notification system for Ruby. (A framework agnostic port of the mack-notifier package.)
Ruby
2
star
71

bostongo

Go
2
star
72

ringo

Go
1
star
73

labs

Go
1
star
74

gjs

HTML
1
star
75

haste

Go
1
star
76

ek

A simple echo server
Go
1
star
77

FakeWeb.js

A port of FakeWeb (Ruby) to help test JavaScript AJAX apps
CoffeeScript
1
star
78

content_o_matic

A gem to download the contents of a page and include them into your application.
Ruby
1
star
79

mytmux

1
star
80

gurl

Go
1
star
81

tt

T.T. The Test Running Bear!
Go
1
star
82

gonit

Go
1
star
83

cleo

Go
1
star
84

buffalo-trash

Go
1
star
85

bloggy

HTML
1
star
86

markbates.github.com

HTML
1
star
87

gentronics

Go
1
star
88

clio

Go
1
star
89

bert

Go
1
star
90

cobble

A very simple and easy library to help facilitate the building and creating of objects.
Ruby
1
star
91

clam

Go
1
star
92

konacha-sinatra

Ruby
1
star
93

hmax

A Golang package to make working with HMAC requests easier.
Go
1
star
94

safe

Go
1
star