Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports loading multiple files #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions lib/settingslogic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion spec/settings.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/settings2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setting2: 8
2 changes: 1 addition & 1 deletion spec/settingslogic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down