lib/ditty/services/settings.rb in ditty-0.7.2 vs lib/ditty/services/settings.rb in ditty-0.8.0

- old
+ new

@@ -14,16 +14,23 @@ # It has the concept of sections which can either be included in the main # settings.yml file, or as separate files in the `config` folder. The values # in separate files will be used in preference of those in the `settings.yml` # file. module Settings - CONFIG_FOLDER = './config'.freeze - CONFIG_FILE = "#{CONFIG_FOLDER}/settings.yml".freeze + CONFIG_FOLDER = './config' + CONFIG_FILE = "#{CONFIG_FOLDER}/settings.yml" class << self def [](key) - values(key.to_sym) + keys = key.to_s.split('.').map(&:to_sym) + from = values + if keys.count > 1 && scope?(keys.first) + from = values(keys.first) + keys = keys[1..-1] + key = keys.join('.') + end + from[key.to_sym] || from.dig(*keys) end def values(scope = :settings) @values ||= begin v = Hash.new do |h, k| @@ -36,9 +43,13 @@ end v[:settings] = File.file?(CONFIG_FILE) ? read(CONFIG_FILE) : {} v end @values[scope] + end + + def scope?(name) + @values.key?(name.to_sym) || File.file?("#{CONFIG_FOLDER}/#{name}.yml") end attr_writer :values def read(filename)