lib/figleaf/settings.rb in figleaf-0.2.5 vs lib/figleaf/settings.rb in figleaf-0.2.6

- old
+ new

@@ -1,8 +1,9 @@ module Figleaf class Settings InvalidYAML = Class.new(RuntimeError) + InvalidRb = Class.new(RuntimeError) class_attribute :auto_define self.auto_define = false class << self @@ -39,17 +40,12 @@ # foo: bar # --> Settings.some_service.foo = bar def load_settings(file_pattern = default_file_pattern, env_to_load = env) configure_with_auto_define do Dir.glob(file_pattern).each do |file| - property_name = File.basename(file, '.yml') - yaml_hash = load_file(file) or next - default = yaml_hash["default"] - property = yaml_hash[env_to_load] + property_name, property = load_file(file, env_to_load) - property = default.merge(property) if !default.nil? - next if property.nil? if self.respond_to?(property_name) && self.send(property_name).respond_to?(:merge) && property.respond_to?(:merge) @@ -59,24 +55,49 @@ self.send("#{property_name}=", property) end end end - def load_file(file_path, env = nil) - property = YAML.load(ERB.new(IO.read(file_path)).result) - property = property[env] if env - use_hashie_if_hash(property) + def load_file(file, env_to_load = env) + if file.end_with?('.rb') + property_name = File.basename(file, '.rb') + config = load_rb_file(file) or return nil + else + property_name = File.basename(file, '.yml') + config = load_yaml_file(file) or return nil + end + + return if config.nil? + + default = config["default"] + property = config[env_to_load] + property = default.merge(property) if !default.nil? + + [property_name, use_hashie_if_hash(property)] + end + + def load_rb_file(file_path, env = nil) + contents = File.read(file_path) + block = ->(*) { eval contents } + Config.new.call(&block) + rescue SyntaxError => e + raise InvalidRb, "#{file_path} has invalid Ruby\n" + e.message + end + + def load_yaml_file(file_path) + YAML.load(ERB.new(IO.read(file_path)).result) rescue Psych::SyntaxError => e raise InvalidYAML, "#{file_path} has invalid YAML\n" + e.message end def root return Rails.root if defined?(Rails) Pathname.new('.') end def default_file_pattern - root.join('config/settings/*.yml') + root.join('config/settings/*.yml') + + root.join('config/settings/*.rb') end def env return Rails.env if defined?(Rails) ENV['ENVIRONMENT']