Skip to content

Commit

Permalink
Added support for loading entire directories of configs.
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickTulskie committed Nov 12, 2012
1 parent f305400 commit dd8880c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
16 changes: 11 additions & 5 deletions lib/configster/configster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,20 @@ def raw_configster
# ===========================
def self.load!(configster_config)
@configster_config ||= { }
new_config = case configster_config
case configster_config
when Hash
configster_config
@configster_config.merge!(configster_config)
when String
YAML.load_file(configster_config)
if File.directory?(configster_config)
Dir.glob(File.join(configster_config, "*.yml")).each do |file|
@configster_config.merge!(YAML.load_file(file))
end
elsif File.exists?(configster_config)
@configster_config.merge!(YAML.load_file(configster_config))
else
raise "Unable to locate #{configster_config}"
end
end

@configster_config.merge!(new_config)
end

def self.config_for(klass)
Expand Down
11 changes: 10 additions & 1 deletion spec/configster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

describe Configster do

subject { Configster }
subject do
Configster.load!(File.join($spec_root, 'configurations', 'test_configuration.yml'))
Configster
end

it { should respond_to(:config_for) }

it "should be able to load the configuration for a specific class" do
Configster.config_for(KonfiguredKlass).should_not be_nil
end

it "should be able to load a directory of yml files" do
Configster.load!(File.join($spec_root, 'configurations'))
Configster.config_for('SecondConfiguration').should_not be_nil
end

it "should not destroy other configurations when loading additional configs" do
Configster.load!('test_thing' => { 'test' => true })
Configster.config_for('test_thing').test.should be_true
Expand Down
4 changes: 4 additions & 0 deletions spec/configurations/second_configuration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SecondConfiguration:
user_name: configster
password: bacon
tag_line: "Okay..."
1 change: 1 addition & 0 deletions spec/konfigured_klass_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
describe KonfiguredKlass do

before(:all) do
Configster.load!(File.join($spec_root, 'configurations', 'test_configuration.yml'))
@test_klass = KonfiguredKlass.new
end

Expand Down
2 changes: 0 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ def p(s)
end
end

Configster.load!(File.join($spec_root, 'configurations', 'test_configuration.yml'))

class KonfiguredKlass
include Configster
end
Expand Down

0 comments on commit dd8880c

Please sign in to comment.