class Global::Backend::Filesystem
Loads Global
configuration from the filesystem
Available options:
-
‘path` (required): the directory with config files
-
‘environment` (required): the environment to load
-
‘yaml_whitelist_classes`: the set of classes that are permitted to unmarshal from the configuration files
For Rails:
-
the ‘path` is optional and defaults to `config/global`
-
the ‘environment` is optional and defaults to the current Rails environment
Constants
- FILE_ENV_SPLIT
- YAML_EXT
Public Class Methods
new(options = {})
click to toggle source
# File lib/global/backend/filesystem.rb, line 20 def initialize(options = {}) if defined?(Rails) @path = options.fetch(:path) { Rails.root.join('config', 'global').to_s } @environment = options.fetch(:environment) { Rails.env.to_s } else @path = options.fetch(:path) @environment = options.fetch(:environment) end @yaml_whitelist_classes = options.fetch(:yaml_whitelist_classes, []) end
Public Instance Methods
load()
click to toggle source
# File lib/global/backend/filesystem.rb, line 31 def load load_from_path(@path) end
Private Instance Methods
get_config_by_key(config, key)
click to toggle source
# File lib/global/backend/filesystem.rb, line 56 def get_config_by_key(config, key) return {} if config.empty? config[key.to_sym] || config[key.to_s] || {} end
load_from_directory(path)
click to toggle source
# File lib/global/backend/filesystem.rb, line 73 def load_from_directory(path) config = {} if File.directory?(path) Dir["#{path}/*"].each do |entry| namespace = File.basename(entry, YAML_EXT) next if namespace.include? FILE_ENV_SPLIT # skip files with dot(s) in name file_with_path = File.join(File.dirname(entry), File.basename(entry, YAML_EXT)) config.deep_merge!(namespace => load_from_path(file_with_path)) end end config end
load_from_file(path)
click to toggle source
# File lib/global/backend/filesystem.rb, line 41 def load_from_file(path) config = {} if File.exist?(file = "#{path}#{YAML_EXT}") configurations = load_yml_file(file) config = get_config_by_key(configurations, 'default') config.deep_merge!(get_config_by_key(configurations, @environment)) if File.exist?(env_file = "#{path}#{FILE_ENV_SPLIT}#{@environment}#{YAML_EXT}") config.deep_merge!(load_yml_file(env_file) || {}) end end config end
load_from_path(path)
click to toggle source
# File lib/global/backend/filesystem.rb, line 37 def load_from_path(path) load_from_file(path).deep_merge(load_from_directory(path)) end
load_yml_file(file)
click to toggle source
# File lib/global/backend/filesystem.rb, line 62 def load_yml_file(file) file_contents = ERB.new(IO.read(file)).result permitted_classes = [Date, Time, DateTime, Symbol].concat(@yaml_whitelist_classes) if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('4') YAML.safe_load(file_contents, permitted_classes: permitted_classes, aliases: true) else YAML.safe_load(file_contents, permitted_classes, [], true) end end