Skip to content

Commit

Permalink
moving to git from svn
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher J. Bottaro committed May 19, 2008
0 parents commit 8293431
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 0 deletions.
1 change: 1 addition & 0 deletions README
56 changes: 56 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
== Summary
Application level configuration.

== Author
Christopher J. Bottaro

=== Accessing the AppConfig object
After installing this plugin, the AppConfig object will be global available. Entries are accessed via object member notation:
AppConfig.my_config_entry
Nested entries are supported:
AppConfig.my_section.some_entry

=== Common config file
Config entries defined in
RAILS_ROOT/config/app_config.yml
will be available to all environments.

=== Environment specific config files
You can have environment specific config files. Environment specific config entries take precedence over common config entries.

Example development environment config file:
RAILS_ROOT/config/environments/development.yml

Example production environment config file:
RAILS_ROOT/config/environments/production.yml

=== Embedded Ruby (ERB)
Embedded Ruby is allowed in the configuration files. See examples below.

=== Examples
Consider the two following config files.

RAILS_ROOT/config/app_config.yml:
size: 1
server: google.com

RAILS_ROOT/config/environments/development.yml:
size: 2
computed: <%= 1 + 2 + 3 %>
section:
size: 3
servers: [ {name: yahoo.com}, {name: amazon.com} ]

Notice that the environment specific config entries overwrite the common entries.
AppConfig.size -> 2
AppConfig.server -> google.com

Notice the embedded Ruby.
AppConfig.computed -> 6

Notice that object member notation is maintained even in nested entries.
AppConfig.section.size -> 3

Notice array notation and object member notation is maintained.
AppConfig.section.servers[0].name -> yahoo.com
AppConfig.section.servers[1].name -> amazon.com
22 changes: 22 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the app_config plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the app_config plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'AppConfig'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
4 changes: 4 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'app_config'

::AppConfig = ApplicationConfig.load_files(RAILS_ROOT+"/config/app_config.yml",
RAILS_ROOT+"/config/environments/#{RAILS_ENV}.yml")
1 change: 1 addition & 0 deletions install.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Install hook code here
41 changes: 41 additions & 0 deletions lib/app_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'ostruct'
require 'yaml'
require 'erb'

# == Summary
# This is API documentation, NOT documentation on how to use this plugin. For that, see the README.
module ApplicationConfig

# Create a config object (OpenStruct) from a yaml file. If a second yaml file is given, then the sections of that file will overwrite the sections
# if the first file if they exist in the first file.
def self.load_files(conf_path_1, conf_path_2 = nil)

conf1 = YAML.load(ERB.new(IO.read(conf_path_1)).result) if conf_path_1 and File.exists?(conf_path_1)
conf1 = {} if !conf1 or conf1.empty?

conf2 = YAML.load(ERB.new(IO.read(conf_path_2)).result) if conf_path_2 and File.exists?(conf_path_2)
conf2 = {} if !conf2 or conf2.empty?

conf = conf1.merge(conf2)
(!conf or conf.empty?) ? OpenStruct.new : convert(conf)

end

# Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)
def self.convert(h) #:nodoc:
s = OpenStruct.new
h.each do |k, v|
s.new_ostruct_member(k)
if v.instance_of?(Hash)
s.send( (k+'=').to_sym, convert(v))
elsif v.instance_of?(Array)
converted_array = v.collect { |e| e.instance_of?(Hash) ? convert(e) : e }
s.send( (k+'=').to_sym, converted_array)
else
s.send( (k+'=').to_sym, v)
end
end
s
end

end
4 changes: 4 additions & 0 deletions tasks/app_config_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :app_config do
# # Task goes here
# end
2 changes: 2 additions & 0 deletions test/app_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
size: 1
server: google.com
44 changes: 44 additions & 0 deletions test/app_config_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'test/unit'
require 'app_config'

class AppConfigTest < Test::Unit::TestCase

def test_missing_files
config = ApplicationConfig.load_files('not_here1', 'not_here2')
assert_equal OpenStruct.new, config
end

def test_empty_files
config = ApplicationConfig.load_files('test/empty1.yml', 'test/empty2.yml')
assert_equal OpenStruct.new, config
end

def test_common
config = ApplicationConfig.load_files('test/app_config.yml')
assert_equal 1, config.size
assert_equal 'google.com', config.server
end

def test_environment_override
config = ApplicationConfig.load_files('test/app_config.yml', 'test/development.yml')
assert_equal 2, config.size
assert_equal 'google.com', config.server
end

def test_nested
config = ApplicationConfig.load_files('test/development.yml')
assert_equal 3, config.section.size
end

def test_array
config = ApplicationConfig.load_files('test/development.yml')
assert_equal 'yahoo.com', config.section.servers[0].name
assert_equal 'amazon.com', config.section.servers[1].name
end

def test_erb
config = ApplicationConfig.load_files('test/development.yml')
assert_equal 6, config.computed
end

end
5 changes: 5 additions & 0 deletions test/development.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
size: 2
computed: <%= 1 + 2 + 3 %>
section:
size: 3
servers: [ {name: yahoo.com}, {name: amazon.com} ]
Empty file added test/empty1.yml
Empty file.
Empty file added test/empty2.yml
Empty file.
1 change: 1 addition & 0 deletions uninstall.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Uninstall hook code here

0 comments on commit 8293431

Please sign in to comment.