2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-07 04:47:26 +08:00
|
|
|
module SiteSettings
|
|
|
|
end
|
|
|
|
|
|
|
|
class SiteSettings::YamlLoader
|
|
|
|
def initialize(file)
|
|
|
|
@file = file
|
|
|
|
end
|
|
|
|
|
|
|
|
def load
|
2022-12-17 04:07:18 +08:00
|
|
|
yaml = load_yaml(@file)
|
2015-04-18 19:53:53 +08:00
|
|
|
yaml.each_key do |category|
|
2013-11-07 04:47:26 +08:00
|
|
|
yaml[category].each do |setting_name, hash|
|
|
|
|
if hash.is_a?(Hash)
|
|
|
|
# Get default value for the site setting:
|
2017-08-07 09:43:09 +08:00
|
|
|
value = hash.delete("default")
|
2018-11-14 15:03:02 +08:00
|
|
|
|
2021-12-14 00:36:29 +08:00
|
|
|
if value.nil?
|
2017-08-16 10:42:08 +08:00
|
|
|
raise StandardError,
|
|
|
|
"The site setting `#{setting_name}` in '#{@file}' is missing default value."
|
2017-08-07 09:43:09 +08:00
|
|
|
end
|
2013-11-07 04:47:26 +08:00
|
|
|
|
2022-07-08 23:00:47 +08:00
|
|
|
if hash.values_at("min", "max").any? && hash["validator"].present?
|
|
|
|
raise StandardError,
|
|
|
|
"The site setting `#{setting_name}` in '#{@file}' will have it's min/max validation ignored because there is a validator also specified."
|
|
|
|
end
|
|
|
|
|
2017-08-07 09:43:09 +08:00
|
|
|
yield category, setting_name, value, hash.deep_symbolize_keys!
|
2013-11-07 04:47:26 +08:00
|
|
|
else
|
|
|
|
# Simplest case. site_setting_name: 'default value'
|
|
|
|
yield category, setting_name, hash, {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-12-17 04:07:18 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_yaml(path)
|
|
|
|
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.1.0")
|
|
|
|
YAML.load_file(path, aliases: true)
|
|
|
|
else
|
|
|
|
YAML.load_file(path)
|
|
|
|
end
|
|
|
|
end
|
2014-10-03 13:53:01 +08:00
|
|
|
end
|