From 2905be85ad784ac559dff704d01f9fbaf76f2015 Mon Sep 17 00:00:00 2001 From: moioo Date: Thu, 8 May 2014 14:54:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/settingslogic.rb | 20 +++++++++++++------- spec/settings.rb | 2 +- spec/settings2.yml | 1 + spec/settingslogic_spec.rb | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 spec/settings2.yml diff --git a/lib/settingslogic.rb b/lib/settingslogic.rb index a99acaf..344c357 100644 --- a/lib/settingslogic.rb +++ b/lib/settingslogic.rb @@ -92,21 +92,27 @@ 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? ? {} : 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 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 From 4a4bd61e0a50105ec5347a74e246548135cab8a1 Mon Sep 17 00:00:00 2001 From: moioo Date: Fri, 22 Jul 2022 19:41:27 +0800 Subject: [PATCH 2/2] support ruby 3.1 --- lib/settingslogic.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/settingslogic.rb b/lib/settingslogic.rb index 344c357..015f0dc 100644 --- a/lib/settingslogic.rb +++ b/lib/settingslogic.rb @@ -104,7 +104,7 @@ def initialize(hash_or_file = self.class.source, section = nil) _hash = {} hash_or_files.each do |file| file_contents = open(file).read - hash = file_contents.empty? ? {} : YAML.load(ERB.new(file_contents).result).to_hash + 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 @@ -116,6 +116,14 @@ def initialize(hash_or_file = self.class.source, section = nil) 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)