Class: Dry::Web::Settings
- Inherits:
-
Object
- Object
- Dry::Web::Settings
- Defined in:
- lib/dry/web/settings.rb
Constant Summary collapse
- SettingValueError =
Class.new(StandardError)
Class Method Summary collapse
- .load(root, env) ⇒ Object
- .schema ⇒ Object
- .setting(name, type = nil) ⇒ Object
- .settings(new_schema) ⇒ Object
Class Method Details
.load(root, env) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/dry/web/settings.rb', line 30 def self.load(root, env) yaml_path = root.join("config/settings.yml") yaml_data = File.exist?(yaml_path) ? YAML.load_file(yaml_path)[env.to_s] : {} schema = self.schema Class.new do extend Dry::Configurable schema.each do |key, type| value = ENV.fetch(key.to_s.upcase) { yaml_data[key.to_s.downcase] } begin value = type[value] if type rescue => e raise SettingValueError, "error typecasting +#{key}+: #{e}" end setting key, value end end.config end |
.schema ⇒ Object
8 9 10 |
# File 'lib/dry/web/settings.rb', line 8 def self.schema @schema ||= {} end |
.setting(name, type = nil) ⇒ Object
12 13 14 |
# File 'lib/dry/web/settings.rb', line 12 def self.setting(name, type = nil) settings(name => type) end |
.settings(new_schema) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/dry/web/settings.rb', line 16 def self.settings(new_schema) check_schema_duplication(new_schema) @schema = schema.merge(new_schema) self end |