hanami 2.0.0.alpha2 → 2.0.0.alpha3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,97 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Hanami
4
- class Application
5
- module Settings
6
- # Default application settings loader. Uses dotenv (if available) to load
7
- # .env files and then loads settings from ENV.
8
- #
9
- # @since 2.0.0
10
- # @api private
11
- class Loader
12
- InvalidSettingsError = Class.new(StandardError) do
13
- def initialize(errors)
14
- @errors = errors
15
- end
16
-
17
- def to_s
18
- <<~STR.strip
19
- Could not initialize settings. The following settings were invalid:
20
-
21
- #{@errors.map { |setting, message| "#{setting}: #{message}" }.join("\n")}
22
- STR
23
- end
24
- end
25
-
26
- UnsupportedSettingArgumentError = Class.new(StandardError) do
27
- def initialize(setting_name, arguments)
28
- @setting_name = setting_name
29
- @arguments = arguments
30
- end
31
-
32
- def to_s
33
- <<~STR.strip
34
- Unsupported arguments #{@arguments.inspect} for setting +#{@setting_name}+
35
- STR
36
- end
37
- end
38
-
39
- def initialize(*)
40
- end
41
-
42
- def call(defined_settings)
43
- load_dotenv
44
-
45
- settings, errors = load_settings(defined_settings)
46
-
47
- raise InvalidSettingsError, errors if errors.any?
48
-
49
- settings
50
- end
51
-
52
- private
53
-
54
- def load_dotenv
55
- require "dotenv"
56
- Dotenv.load(*dotenv_files) if defined?(Dotenv)
57
- rescue LoadError # rubocop:disable Lint/SuppressedException
58
- end
59
-
60
- def dotenv_files
61
- [
62
- ".env.#{Hanami.env}.local",
63
- (".env.local" unless Hanami.env?(:test)),
64
- ".env.#{Hanami.env}",
65
- ".env"
66
- ].compact
67
- end
68
-
69
- def load_settings(defined_settings) # rubocop:disable Metrics/MethodLength
70
- defined_settings.each_with_object([{}, {}]) { |(name, args), (settings, errors)|
71
- begin
72
- settings[name] = resolve_setting(name, args)
73
- rescue => exception # rubocop:disable Style/RescueStandardError
74
- if exception.is_a?(UnsupportedSettingArgumentError) # rubocop: disable Style/GuardClause
75
- raise exception
76
- else
77
- errors[name] = exception
78
- end
79
- end
80
- }
81
- end
82
-
83
- def resolve_setting(name, args)
84
- value = ENV.fetch(name.to_s.upcase) { Undefined }
85
-
86
- if args.none?
87
- value
88
- elsif args[0]&.respond_to?(:call)
89
- args[0].call(value)
90
- else
91
- raise UnsupportedSettingArgumentError.new(name, args)
92
- end
93
- end
94
- end
95
- end
96
- end
97
- end
@@ -1,65 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Hanami
4
- class Application
5
- module Settings
6
- # Application settings struct
7
- #
8
- # When the application loads settings, a struct subclass is created for
9
- # the settings defined specifically for the application, then initialized
10
- # with those settings and their values
11
- #
12
- # @since 2.0.0
13
- # @api public
14
- class Struct
15
- class << self
16
- def [](names)
17
- Class.new(self) do
18
- @setting_names = names
19
-
20
- define_singleton_method(:setting_names) do
21
- @setting_names
22
- end
23
-
24
- define_readers
25
- end
26
- end
27
-
28
- def reserved?(name)
29
- instance_methods.include?(name)
30
- end
31
-
32
- private
33
-
34
- def define_readers
35
- setting_names.each do |name|
36
- next if reserved?(name)
37
-
38
- define_method(name) do
39
- @settings[name]
40
- end
41
- end
42
- end
43
- end
44
-
45
- def initialize(settings)
46
- @settings = settings.freeze
47
- end
48
-
49
- def [](name)
50
- raise ArgumentError, "Unknown setting +#{name}+" unless self.class.setting_names.include?(name)
51
-
52
- if self.class.reserved?(name)
53
- @settings[name]
54
- else
55
- public_send(name)
56
- end
57
- end
58
-
59
- def to_h
60
- @settings.to_h
61
- end
62
- end
63
- end
64
- end
65
- end