Skip to content

Commit

Permalink
Added Configatron::Rails.init helper method and ConfigatronGenerator …
Browse files Browse the repository at this point in the history
…for generating configatron files for Rails.
  • Loading branch information
markbates committed Aug 26, 2009
1 parent 6f16c72 commit 26be433
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ require 'gemstub'
Gemstub.test_framework = :rspec

Gemstub.gem_spec do |s|
s.version = "2.3.2"
s.version = "2.4.0"
s.summary = "A powerful Ruby configuration system."
s.rubyforge_project = "magrathea"
s.add_dependency('yamler', '>=0.1.0')
s.email = 'mark@markbates.com'
s.homepage = 'http://www.metabates.com'
s.files = FileList['lib/**/*.*', 'README', 'LICENSE', 'bin/**/*.*', 'generators/**/*.*']
end

Gemstub.rdoc do |rd|
Expand Down
6 changes: 3 additions & 3 deletions configatron.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

Gem::Specification.new do |s|
s.name = %q{configatron}
s.version = "2.3.2.20090731134515"
s.version = "2.4.0.20090826115559"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["markbates"]
s.date = %q{2009-07-31}
s.date = %q{2009-08-26}
s.description = %q{configatron was developed by: markbates}
s.email = %q{mark@markbates.com}
s.extra_rdoc_files = ["README", "LICENSE"]
s.files = ["lib/configatron/configatron.rb", "lib/configatron/core_ext/class.rb", "lib/configatron/core_ext/kernel.rb", "lib/configatron/core_ext/object.rb", "lib/configatron/core_ext/string.rb", "lib/configatron/errors.rb", "lib/configatron/store.rb", "lib/configatron.rb", "README", "LICENSE"]
s.files = ["lib/configatron/configatron.rb", "lib/configatron/core_ext/class.rb", "lib/configatron/core_ext/kernel.rb", "lib/configatron/core_ext/object.rb", "lib/configatron/core_ext/string.rb", "lib/configatron/errors.rb", "lib/configatron/rails.rb", "lib/configatron/store.rb", "lib/configatron.rb", "README", "LICENSE", "generators/configatron_generator.rb", "generators/templates/configatron/cucumber.rb", "generators/templates/configatron/defaults.rb", "generators/templates/configatron/development.rb", "generators/templates/configatron/production.rb", "generators/templates/configatron/test.rb", "generators/templates/initializers/configatron.rb"]
s.homepage = %q{http://www.metabates.com}
s.require_paths = ["lib"]
s.rubyforge_project = %q{magrathea}
Expand Down
20 changes: 20 additions & 0 deletions generators/configatron_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'rails_generator'
class ConfigatronGenerator < Rails::Generator::Base # :nodoc:

def manifest
record do |m|
m.directory 'config/configatron'
m.directory 'config/configatron/development'
m.directory 'config/configatron/test'
m.directory 'config/configatron/production'
m.directory 'config/configatron/cucumber'
m.file 'initializers/configatron.rb', 'config/initializers/configatron.rb'
m.file 'configatron/development.rb', 'config/configatron/development.rb'
m.file 'configatron/production.rb', 'config/configatron/production.rb'
m.file 'configatron/test.rb', 'config/configatron/test.rb'
m.file 'configatron/cucumber.rb', 'config/configatron/cucumber.rb'
m.file 'configatron/defaults.rb', 'config/configatron/defaults.rb'
end
end

end # ConfigatronGenerator
4 changes: 4 additions & 0 deletions generators/templates/configatron/cucumber.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Override your default settings for the Cucumber environment here.

# By default load in the settings from the test environment:
require File.join(File.dirname(__FILE__), 'test')
7 changes: 7 additions & 0 deletions generators/templates/configatron/defaults.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Put all your default configatron settings here.

# Example:
# configatron.emails.welcome.subject = 'Welcome!'
# configatron.emails.sales_reciept.subject = 'Thanks for your order'
#
# configatron.file.storage = :s3
4 changes: 4 additions & 0 deletions generators/templates/configatron/development.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Override your default settings for the Development environment here.
#
# Example:
# configatron.file.storage = :local
4 changes: 4 additions & 0 deletions generators/templates/configatron/production.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Override your default settings for the Production environment here.
#
# Example:
# configatron.file.storage = :s3
4 changes: 4 additions & 0 deletions generators/templates/configatron/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Override your default settings for the Test environment here.
#
# Example:
# configatron.file.storage = :local
2 changes: 2 additions & 0 deletions generators/templates/initializers/configatron.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'configatron'
Configatron::Rails.init
3 changes: 2 additions & 1 deletion lib/configatron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
require File.join(base, 'core_ext', 'kernel')
require File.join(base, 'core_ext', 'object')
require File.join(base, 'core_ext', 'string')
require File.join(base, 'core_ext', 'class')
require File.join(base, 'core_ext', 'class')
require File.join(base, 'rails')
10 changes: 7 additions & 3 deletions lib/configatron/configatron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ def temp_end
@_store.delete(@_namespace.pop)
end

undef :inspect # :nodoc:
undef :nil? # :nodoc:
undef :test # :nodoc:
begin
undef :inspect # :nodoc:
undef :nil? # :nodoc:
undef :test # :nodoc:
rescue Exception => e
end


end
44 changes: 44 additions & 0 deletions lib/configatron/rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Configatron
# Helpful for making using configatron with Rails even easier!
#
# To get started you can use the generator to generate
# the necessary stub files.
#
# $ ruby script/generate configatron
module Rails

# Loads configatron files in the following order:
#
# Example:
# <RAILS_ROOT>/config/configatron/defaults.rb
# <RAILS_ROOT>/config/configatron/<RAILS_ENV>.rb
# # optional:
# <RAILS_ROOT>/config/configatron/<RAILS_ENV>/defaults.rb
# <RAILS_ROOT>/config/configatron/<RAILS_ENV>/bar.rb
# <RAILS_ROOT>/config/configatron/<RAILS_ENV>/foo.rb
def self.init
config_files = []

base_dir = File.expand_path(File.join(RAILS_ROOT, 'config', 'configatron'))
config_files << File.join(base_dir, 'defaults.rb')
config_files << File.join(base_dir, "#{RAILS_ENV}.rb")

env_dir = File.join(base_dir, RAILS_ENV)
config_files << File.join(env_dir, 'defaults.rb')

Dir.glob(File.join(env_dir, '*.rb')).sort.each do |f|
config_files << f
end

config_files.collect! {|config| File.expand_path(config)}.uniq!

config_files.each do |config|
if File.exists?(config)
# puts "Configuration: #{config}"
require config
end
end
end

end # Rails
end # Configatron
6 changes: 5 additions & 1 deletion lib/configatron/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@ def parse_options(options)
end
end

undef :test # :nodoc:
begin
undef :test # :nodoc:
rescue Exception => e
end


end # Store
end # Configatron
67 changes: 67 additions & 0 deletions spec/configatron/rails_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Configatron::Rails do

%w{development test production cucumber}.each do |env|

describe env do

before(:each) do
Object.send(:remove_const, 'RAILS_ENV') rescue nil
Object.send(:remove_const, 'RAILS_ROOT') rescue nil
RAILS_ENV = env
RAILS_ROOT = File.join(File.dirname(__FILE__), '..', "tmp_rails_root_#{env}")
@configatron_path = File.join(RAILS_ROOT, 'config', 'configatron')
@defaults_file_loc = File.join(@configatron_path, 'defaults.rb')
@env_file_loc = File.join(@configatron_path, "#{env}.rb")
@env_folder_loc = File.join(@configatron_path, env)
@env_bar_loc = File.join(@env_folder_loc, 'bar.rb')
@env_foo_loc = File.join(@env_folder_loc, 'foo.rb')

FileUtils.mkdir_p(@env_folder_loc)

File.open(@defaults_file_loc, 'w') do |f|
f.puts 'configatron.fooa = :foo'
f.puts 'configatron.bara = :bar'
f.puts 'configatron.env = :default'
f.puts 'configatron.something.else = 1'
f.puts 'configatron.and.another.thing = 42'
end

File.open(@env_file_loc, 'w') do |f|
f.puts "configatron.env = :#{env}"
f.puts 'configatron.something.else = 2'
end

File.open(@env_bar_loc, 'w') do |f|
f.puts "configatron.bara = 'BAR!!'"
f.puts 'configatron.something.else = 3'
end

File.open(@env_foo_loc, 'w') do |f|
f.puts "configatron.fooa = :fubar"
f.puts 'configatron.something.else = 4'
end
end

after(:each) do
FileUtils.rm_rf(RAILS_ROOT)
Object.send(:remove_const, 'RAILS_ENV')
Object.send(:remove_const, 'RAILS_ROOT')
# configatron.reset!
end

it 'should read the defaults first and then the env file' do
Configatron::Rails.init
configatron.fooa.should == :fubar
configatron.bara.should == 'BAR!!'
configatron.env.should == env.to_sym
configatron.something.else.should == 4
configatron.and.another.thing.should == 42
end

end

end

end

0 comments on commit 26be433

Please sign in to comment.