diff --git a/lib/settingslogic.rb b/lib/settingslogic.rb index a99acaf..015f0dc 100644 --- a/lib/settingslogic.rb +++ b/lib/settingslogic.rb @@ -92,24 +92,38 @@ def create_accessor_for(key) # if you are using this in rails. If you pass a string it should be an absolute path to your settings file. # Then you can pass a hash, and it just allows you to access the hash via methods. def initialize(hash_or_file = self.class.source, section = nil) - #puts "new! #{hash_or_file}" + puts "new! #{hash_or_file}" case hash_or_file when nil raise Errno::ENOENT, "No file specified as Settingslogic source" when Hash + puts "hash! #{hash_or_file}" self.replace hash_or_file else - file_contents = open(hash_or_file).read - hash = file_contents.empty? ? {} : YAML.load(ERB.new(file_contents).result).to_hash - if self.class.namespace - hash = hash[self.class.namespace] or return missing_key("Missing setting '#{self.class.namespace}' in #{hash_or_file}") + hash_or_files = hash_or_file.is_a?(Array) ? hash_or_file : [hash_or_file] + _hash = {} + hash_or_files.each do |file| + file_contents = open(file).read + hash = file_contents.empty? ? {} : initialize_yml(file_contents) #YAML.load(ERB.new(file_contents).result).to_hash + if self.class.namespace + hash = hash[self.class.namespace] or return missing_key("Missing setting '#{self.class.namespace}' in #{file}") + end + _hash.merge!(hash) end - self.replace hash + self.replace _hash end - @section = section || self.class.source # so end of error says "in application.yml" + @section = section || (self.class.source.is_a?(Array) ? self.class.source.join(',') : self.class.source) # so end of error says "in application.yml" create_accessors! end + def initialize_yml(content) + begin + YAML.load(ERB.new(content).result, aliases: true).to_hash + rescue ArgumentError + YAML.load(ERB.new(content).result).to_hash + end + end + # Called for dynamically-defined keys, and also the first key deferenced at the top-level, if load! is not used. # Otherwise, create_accessors! (called by new) will have created actual methods for each key. def method_missing(name, *args, &block) diff --git a/spec/settings.rb b/spec/settings.rb index 8c5be68..c08f15e 100644 --- a/spec/settings.rb +++ b/spec/settings.rb @@ -1,5 +1,5 @@ class Settings < Settingslogic - source "#{File.dirname(__FILE__)}/settings.yml" + source ["#{File.dirname(__FILE__)}/settings.yml","#{File.dirname(__FILE__)}/settings2.yml"] end class SettingsInst < Settingslogic diff --git a/spec/settings2.yml b/spec/settings2.yml new file mode 100644 index 0000000..ffa8fd2 --- /dev/null +++ b/spec/settings2.yml @@ -0,0 +1 @@ +setting2: 8 diff --git a/spec/settingslogic_spec.rb b/spec/settingslogic_spec.rb index 34e4798..17d736d 100644 --- a/spec/settingslogic_spec.rb +++ b/spec/settingslogic_spec.rb @@ -2,7 +2,7 @@ describe "Settingslogic" do it "should access settings" do - Settings.setting2.should == 5 + Settings.setting2.should == 8 end it "should access nested settings" do