• This repository has been archived on 17/Jan/2018
  • Stars
    star
    128
  • Rank 281,044 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 16 years ago
  • Updated over 7 years ago

Reviews

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

Repository Details

Simple Config is a plugin designed to make application-wide configuration settings easy to set and access in an object-oriented fashion.

Simple Config is a plugin designed to make application-wide configuration settings easy to set and access in an object-oriented fashion.

This library was originally designed to be a Rails plugin, but it's now a standard Ruby library with no dependency on Rails. You can use it in any Ruby application or project.

Rails Configuration vs SimpleConfig

Rails already provides a way of configuring the framework on a per-environment or application basis, but the more the application becomes complex, the more the feature shows its limit.

One common solution is to put your app configuration into YAML files and load them somewhere in your environment, but when you have many developers and dynamic configurations this is not always the best choice.

Compared to the default Rails configuration system, SimpleConfig provides the following additional features:

  • Ability to define per developer settings using the local.rb file
  • Ability to nest configurations in groups
  • Ability to clone configs
  • Ability to load unlimited configuration scripts

Getting started

The plugin comes with a rake task to get you up and running quickly, so start by running that.

$ rake simpleconfig:setup

This will create a config/settings folder and a blank settings file for each of the main Rails environments. It will also create a copy of the SimpleConfig initializer in config/initializers/configuration.rb.

Now, if you open up the configuration.rb initializer, you will see something like this:

SimpleConfig.for :application do

  # your app configuration here
  
  load File.join(Rails.root, "config", "settings", "application.rb"),   :if_exists? => true
  load File.join(Rails.root, "config", "settings", "#{RAILS_ENV}.rb"),  :if_exists? => true
  load File.join(Rails.root, "config", "settings", "local.rb"),         :if_exists? => true
  
end

This is where you can set any configuration variables that are required across all Rails environments. The load method works just like Ruby's built-in load method, except the contents of the file it loads are evaluated within the context of the SimpleConfig.for block. The :if_exists? flag, when set to true, means that the file will only be loaded if it exists, otherwise it will simply be ignored.

Variables can be overwritten, and are defined in the order that they are loaded, so you can set up default values in the above file and override them in the environment files.

As well as loading a settings file for your current Rails environment, a file called "local.rb" is loaded which is designed as a place for you to override variables specific to your own development environment -- you can just keep a copy of this locally without having to check it into your version control system[1].

Variables

Setting Variables

Setting variables is simple and will be familiar to anybody who has used Capistrano. Whether in your main SimpleConfig.for block in configuration.rb, or one of your external settings files, use the set method:

SimpleConfig.for :application do
  set :my_variable, 'hello world'
end

SimpleConfig also supports a form of namespacing that allows you to group logical sets of variables together:

SimpleConfig.for :application do
  group :awesome_stuff do
    set :my_variable, 'hello world'
  end
end

Both the set and load methods are available within group blocks and files loaded inside groups will be evaluated in the context of that group.

Whilst I'd recommend not nesting your groups more than one-level, there is no limit on how deep they can be nested.

Unsetting variables

Sometimes you might want to completely delete a variable from the collection. Simply setting its value to nil doesn't work because nil might be a valid value. You can delete a variable using the unset method.

SimpleConfig.for :application do
  set :my_variable, 'hello world'
  
  ...
  
  unset :my_variable
end

For instance, this is useful to remove global settings at environment level instead of overwriting the default value with a nonsense-one. unset returns the value of the variable just in case you need to use it elsewhere.

Does a specific variable exist?

I don't know but you can check it yourself using exists? method.

config = SimpleConfig.for(:application) do
  set :my_variable, 'hello world'
end

# write some nice code 
config.exists? :my_variable     # => true
config.exists? :your_variable   # => false

Accessing your configuration

SimpleConfig allows you set as many separate configurations as you like using the SimpleConfig.for method, which takes a symbol representing the configuration name, although most people will just create a single "application" config as above. To access this config from anywhere in your application, you can also use SimpleConfig.for method without a block, which always returns the named configuration object.

It is worth pointing out that SimpleConfig.for provides an almost singleton-style access to a particular named config. Calling SimpleConfig.for with a block a second time for a particular named configuration will simply extend the existing configuration, not overwrite it.

Once you have a reference to your configuration object, you can access variables using method access. Given the above example, :my_variable would be accessed in the following way:

config = SimpleConfig.for(:application)
config.my_variable # => "hello world"

Accessing grouped variables works as you would expect:

config = SimpleConfig.for(:application)
config.awesome_stuff.my_variable # => "hello world"

Using your configuration in your Rails app

When the application is initalized, by default the configurations are stored in the :application stack. You can access them anywhere using

SimpleConfig.for(:application)

It's a common habit to define a config method in your Rails application or Rails libraries to have quick access to the configuration object. You can also use a mixin.

module Configurable
  def config
    SimpleConfig.for(:application)
  end
end

class ApplicationController < ActionController::Base
  extend  Configurable
  include Configurable

  def do_something
    # here you can use config
    if config.my_variable 
      render :foo
    end
      render :bar
    else
  end

end

An other very common pattern is to assing your configuration object to a constant so that it becomes globally available in your Rails project.

# config/initializers/configuration.rb
# after the initialization block
CONFIG = SimpleConfig.for :app

Then anywhere in your app

def do_something
  if CONFIG.my_variable 
    render :foo
  end
    render :bar
  else
end

fn1(footnote). In fact, I recommend you make sure your version control system ignores this file otherwise you risk checking in a file that will override values in production!

More Repositories

1

LRResty

Yet another Objective-C REST client library, inspired by Ruby's restclient gem.
Objective-C
461
star
2

LROAuth2Client

OAuth2 client for iPhone and iPad apps
Objective-C
303
star
3

betabuilder

A collection of Rake tasks for managing and distributing iOS ad-hoc builds
Ruby
214
star
4

LRSlidingTableViewCell

A simple implementation of sliding table cells, ala Twitter for iPhone
Objective-C
189
star
5

RestfulCoreData

An attempt to come up with a sane web API to Core Data synching pattern, using the Pivotal Tracker API.
Objective-C
155
star
6

mimic

A Ruby gem for faking external web services for testing
Ruby
150
star
7

xcodebuild-rb

Ruby
143
star
8

clickatell

NO LONGER SUPPORTED - Ruby interface to the Clickatell SMS Gateway API
Ruby
138
star
9

swift-responsive-textfield

A SwiftUI wrapper around UITextField with binding-based state and responder control
Swift
86
star
10

xcodesnippets

A command-line utility for managing Xcode 4 code snippets
Ruby
85
star
11

LROAuth2Demo

A demo project for the LROAuth2Client library
Objective-C
59
star
12

LRTableModel

The missing abstraction between UITableView, it's data source, and your domain model
Objective-C
52
star
13

LRMocky

A port of jMock 2.0 for Objective-C
Perl
38
star
14

PusherChat-iPhone

An iPhone client for the PusherChat-Rails example app, using libPusher
Objective-C
35
star
15

LRToolkit

A collection of useful classes and categories I use in my iPhone and iPad projects
Objective-C
32
star
16

session-timeout

A simple dynamic session expiry/timeout plugin for Rails
Ruby
27
star
17

LRLinkableLabel

UILabel replacement with support for linkable strings
Objective-C
25
star
18

LRFetchedResultSet

Objective-C
21
star
19

AutomationKit

An automated UI testing framework for iOS
Objective-C
20
star
20

LRMimic

An Objective-C client for the Mimic REST API
Objective-C
17
star
21

hsbcscraper

Web scraper for downloading statements from HSBC Business Banking
Ruby
15
star
22

swift-coding

A protocol-witness oriented library built on top of Codable
Swift
14
star
23

swift-validations

A high-level functional validation library, written in Swift
Swift
13
star
24

oauth-pkce-ios-client

A small example of how to use OAuth in a SwiftUI app with PKCE
Swift
13
star
25

beanstalk-messaging

Ruby
12
star
26

xcodebuild-outputparser

A small Ruby library for parsing the results of the xcodebuild command, particularly tests
Ruby
10
star
27

iPhoneAuctionSniper

An example of TDD iPhone development using mocks and Cucumber for high-level tests
Objective-C
9
star
28

ripperhud-toolkit

A Sinatra app and collection of Ruby scripts to help organise your downloaded and ripped DVD collection
8
star
29

ujs4rails

Unobtrusive Javascript for Rails
Ruby
8
star
30

oauth-pkce-proxy

Proof of concept OAuth provider proxy server that adds PKCE support for public clients
Ruby
8
star
31

LRRemoteImage

A really simple class to handle remote images
Objective-C
7
star
32

coopexport

Statement exporter for The Co-Operative Bank internet banking website
Ruby
7
star
33

dotfiles

My public dotfiles
Shell
7
star
34

uiautomation-rb

Ruby
6
star
35

SqueezeSlaveMenu

An OSX menu bar app for controlling SqueezeSlave
Objective-C
5
star
36

AdventOfCode2022

Swift
4
star
37

github-downloads

A simple gem for managing your Github project uploads
Ruby
4
star
38

blogdata

Data for my Marley-powered blog
3
star
39

tca-navigation-demo

Experimenting with swiftui-navigation and TCA.
Swift
3
star
40

AdventOfCode2021

Swift
3
star
41

TCANavigationStacks

Exploring the new iOS 16 APIs with TCA.
Swift
2
star
42

LRRestyMacDemo

A Mac demo project that uses LRResty as a framework
Objective-C
2
star
43

Bundler.tmbundle

Ruby
2
star
44

minisculus

Ruby
2
star
45

moviesort

A ruby script for sorting TV shows
Ruby
2
star
46

mpsontwitter.co.uk

Source code for mpsontwitter.co.uk
Ruby
2
star
47

lukeredpath

2
star
48

appium-ios-examples

Examples of using the appium-ios-driver library
Ruby
1
star
49

.vim

Vim Script
1
star
50

LRMiniServerKit

1
star
51

squeezeslave

A Git mirror of the Squeezeslave project
C
1
star
52

musickit-graphql

An experimental GraphQL wrapper for the MusicKit REST API, written in Ruby
Ruby
1
star
53

MimicCocoaSpecDemo

An example of using the Mimic gem to stub network requests from within an Objective-C test case
C
1
star
54

LRBindableObjects

THIS IS JUST A PROOF OF CONCEPT - DO NOT USE!
Objective-C
1
star