Project

bragi-app

0.04
No commit activity in last 3 years
No release in over 3 years
Move the config out of your app, and into App. Sure, it's been done before, and others will do it again, but this is my way, and I like it.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.12.0
 Project Readme

App¶ ↑

Move the config out of your app, and into App.

WARNING:¶ ↑

This release is new, improved, and almost completely incompatible with the gem formerly known as App. (See “What about YAML?”, below.)

K.¶ ↑

Rails (3.0)¶ ↑

% echo 'gem "app"' >> Gemfile
% bundle install
% rails g configurable

You now have App config files at config/app. It will generate an override per Rails environment.

# config/app.rb
class App < Configurable
  # Settings in config/app/* take precedence over those specified here.

  # config.key = "value"

  config.javascript_expansions = {
    :prototype     => "prototype"
    :scriptaculous => %w(effects dragdrop controls)
  }
end

Need to update those sources for production?

# config/app/production.rb
App.configure do
  # Settings specified here will take precedence over those in config/app.rb

  # config.key = "value"

  ajax_lib_base = "http://ajax.googleapis.com/ajax/libs"

  config.javascript_expansions.update({
    :prototype     => "#{ajax_lib_base}/prototype/1.6.1.0/prototype.js"
    :scriptaculous => "#{ajax_lib_base}/scriptaculous/1.8.3/scriptaculous.js"
  })
end

Ruby¶ ↑

App can be used in any Ruby application. Just bundle (see “Rails”, above, less the rails g) or install the gem.

Make sure “app” is required in your environment, and define a class from there.

class MySite < Configurable
  config.launched_at = Time.now.utc
end

What about YAML?¶ ↑

YAML is great, really, but it just wasn’t great beyond simple configuration.

Take that last example. ERB would be required, and still it’s cumbersome.

# app.yml
---
launched_at: <%= Time.now.utc.iso8601 %>

Forget the ISO 8601 formatting and you’ll get a string back, instead.

But that’s just the surface. Where you’re manipulating the primitive objects that YAML supports most easily, you could have stored complex Ruby objects: Pathname, Proc, or anything else.

class App < Configurable
  config.config_path = Rails.root.join "config"
  config.uptime = Proc.new { (Time.now.utc - App.launched_at).seconds }
  config.asset_host = ActiveSupport::StringInquirer.new "amazon"
end

App.config_path.join("app") # => #<Pathname:config/app>
App.uptime.call             # => 430.198400 seconds
App.asset_host.amazon?      # => true

And because these configuration files are just class definitions, you can write your own methods, as well.

class App < Configurable
  def self.destroy_sessions!
    Session.delete_all
  end
end

App.destroy_sessions! # => 1

If Ruby can handle it, so can App.

But OK, OK, there’s still an upgrade path:

class App < Configurable
  # This must come first.
  self.config = <<YAML
legacy: <%= "configuration" %>
YAML

  # config.key = "value"
end

This is not backwards-compatible. This is only to help ease the transition. You will most likely have to upgrade your calls.

LICENSE¶ ↑

(The MIT License)

© 2009-2010 Stephen Celis, stephen@stephencelis.com.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.