ultra_settings 0.0.1.rc1 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "super_settings"
3
+ require "erb"
4
+ require "yaml"
5
+ require "time"
6
+ require "pathname"
7
+ require "singleton"
4
8
 
5
9
  require_relative "ultra_settings/configuration"
10
+ require_relative "ultra_settings/coerce"
6
11
  require_relative "ultra_settings/field"
12
+ require_relative "ultra_settings/rack_app"
13
+ require_relative "ultra_settings/web_view"
14
+ require_relative "ultra_settings/yaml_config"
15
+ require_relative "ultra_settings/version"
16
+
17
+ if defined?(Rails::Railtie)
18
+ require_relative "ultra_settings/railtie"
19
+ end
7
20
 
8
21
  # This is the root namespace for UltraSettings. You can add configurations to
9
22
  # this namespace using the add method.
@@ -12,11 +25,11 @@ require_relative "ultra_settings/field"
12
25
  # UltraSettings.add(:test)
13
26
  # UltraSettings.test # => TestConfiguration.instance
14
27
  module UltraSettings
28
+ VALID_NAME__PATTERN = /\A[a-z_][a-zA-Z0-9_]*\z/
29
+
15
30
  @configurations = {}
16
31
  @mutex = Mutex.new
17
-
18
- class NonStaticValueError < StandardError
19
- end
32
+ @runtime_settings = nil
20
33
 
21
34
  class << self
22
35
  # Adds a configuration to the root namespace. The configuration will be
@@ -29,15 +42,15 @@ module UltraSettings
29
42
  # @return [void]
30
43
  def add(name, klass = nil)
31
44
  name = name.to_s
32
- unless name.match?(/\A[a-z_][a-zA-Z0-9_]*\z/)
45
+ unless name.match?(VALID_NAME__PATTERN)
33
46
  raise ArgementError.new("Invalid configuration name: #{name.inspect}")
34
47
  end
35
48
 
36
49
  class_name = klass&.to_s
37
- class_name ||= "#{name.classify}Configuration"
50
+ class_name ||= "#{classify(name)}Configuration"
38
51
 
39
52
  @mutex.synchronize do
40
- @configurations.delete(name)
53
+ @configurations[name] = class_name
41
54
 
42
55
  eval <<-RUBY, binding, __FILE__, __LINE__ + 1 # rubocop:disable Security/Eval
43
56
  def #{name}
@@ -45,93 +58,158 @@ module UltraSettings
45
58
  end
46
59
  RUBY
47
60
  end
61
+ end
48
62
 
49
- # Control if settings can be loaded from environment variables. By default
50
- # environment variables are enabled. This can also be disabled on
51
- # individual Configuration classes.
52
- #
53
- # @param value [Boolean] Whether or not to load settings from environment variables.
54
- # @return [void]
55
- def environment_variables_disabled=(value)
56
- Configuration.environment_variables_disabled = !!value
57
- end
63
+ # Returns true if the provided class has been added as a configuration.
64
+ #
65
+ # @param class_name [Class, String] The name of the configuration class.
66
+ # @return [Boolean]
67
+ def include?(class_name)
68
+ @configurations.values.collect(&:to_s).include?(class_name.to_s)
69
+ end
58
70
 
59
- # Control if settings can be loaded from runtime settings. By default
60
- # runtime settings are enabled. This can also be disabled on individual
61
- # Configuration classes.
62
- #
63
- # @param value [Boolean] Whether or not to load settings from runtime settings.
64
- # @return [void]
65
- def runtime_settings_disabled=(value)
66
- Configuration.runtime_settings_disabled = !!value
67
- end
71
+ # Control if settings can be loaded from environment variables. By default
72
+ # environment variables are enabled. This can also be disabled on
73
+ # individual Configuration classes.
74
+ #
75
+ # @param value [Boolean] Whether or not to load settings from environment variables.
76
+ # @return [void]
77
+ def environment_variables_disabled=(value)
78
+ Configuration.environment_variables_disabled = !!value
79
+ end
68
80
 
69
- # Control if settings can be loaded from YAML configuration files. By
70
- # default YAML configuration is enabled. This can also be disabled on
71
- # individual Configuration classes.
72
- #
73
- # @param value [Boolean] Whether or not to load settings from YAML configuration.
74
- # @return [void]
75
- def yaml_config_disabled=(value)
76
- Configuration.yaml_config_disabled = !!value
77
- end
81
+ # Control if settings can be loaded from runtime settings. By default
82
+ # runtime settings are enabled. This can also be disabled on individual
83
+ # Configuration classes.
84
+ #
85
+ # @param value [Boolean] Whether or not to load settings from runtime settings.
86
+ # @return [void]
87
+ def runtime_settings_disabled=(value)
88
+ Configuration.runtime_settings_disabled = !!value
89
+ end
78
90
 
79
- # Set the delimiter to use when determining environment variable names.
80
- # By default this is an underscore.
81
- #
82
- # @param value [String] The delimiter to use.
83
- # @return [void]
84
- def env_var_delimiter=(value)
85
- Configuration.env_var_delimiter = value.to_s
86
- end
91
+ # Control if settings can be loaded from YAML configuration files. By
92
+ # default YAML configuration is enabled. This can also be disabled on
93
+ # individual Configuration classes.
94
+ #
95
+ # @param value [Boolean] Whether or not to load settings from YAML configuration.
96
+ # @return [void]
97
+ def yaml_config_disabled=(value)
98
+ Configuration.yaml_config_disabled = !!value
99
+ end
87
100
 
88
- # Set the delimiter to use when determining setting names. By default
89
- # this is a period.
90
- #
91
- # @param value [String] The delimiter to use.
92
- def setting_delimiter=(value)
93
- Configuration.setting_delimiter = value.to_s
94
- end
101
+ # Set the environment to use when loading YAML configuration files.
102
+ # In a Rails application this will be the current Rails environment.
103
+ # Defaults to "development".
104
+ #
105
+ # @param value [String] The environment name to use.
106
+ def yaml_config_env=(value)
107
+ Configuration.yaml_config_env = value
108
+ end
95
109
 
96
- # Control if environment variable names should be upcased. By default
97
- # this is true.
98
- #
99
- # @param value [Boolean] Whether or not to upcase environment variable names.
100
- # @return [void]
101
- def env_var_upcase=(value)
102
- Configuration.env_var_upcase = !!value
103
- end
110
+ # Set the delimiter to use when determining environment variable names.
111
+ # By default this is an underscore.
112
+ #
113
+ # @param value [String] The delimiter to use.
114
+ # @return [void]
115
+ def env_var_delimiter=(value)
116
+ Configuration.env_var_delimiter = value.to_s
117
+ end
118
+
119
+ # Set the delimiter to use when determining setting names. By default
120
+ # this is a period.
121
+ #
122
+ # @param value [String] The delimiter to use.
123
+ def runtime_setting_delimiter=(value)
124
+ Configuration.runtime_setting_delimiter = value.to_s
125
+ end
126
+
127
+ # Control if environment variable names should be upcased. By default
128
+ # this is true.
129
+ #
130
+ # @param value [Boolean] Whether or not to upcase environment variable names.
131
+ # @return [void]
132
+ def env_var_upcase=(value)
133
+ Configuration.env_var_upcase = !!value
134
+ end
135
+
136
+ # Control if setting names should be upcased. By default this is false.
137
+ #
138
+ # @param value [Boolean] Whether or not to upcase setting names.
139
+ # @return [void]
140
+ def runtime_setting_upcase=(value)
141
+ Configuration.runtime_setting_upcase = !!value
142
+ end
143
+
144
+ # Set the directory to use when loading YAML configuration files.
145
+ # In a Rails application this will be the config directory.
146
+ # Otherwise it will be the current working directory.
147
+ #
148
+ # @param value [String, Pathname] The directory to use.
149
+ # @return [void]
150
+ def yaml_config_path=(value)
151
+ Configuration.yaml_config_path = value.to_s
152
+ end
153
+
154
+ # Set the object to use for runtime settings. This can be any object that
155
+ # responds to the [] method. If you are using the `super_settings` gem,
156
+ # you can set this to `SuperSettings`.
157
+ attr_writer :runtime_settings
104
158
 
105
- # Control if setting names should be upcased. By default this is false.
106
- #
107
- # @param value [Boolean] Whether or not to upcase setting names.
108
- # @return [void]
109
- def setting_upcase=(value)
110
- Configuration.setting_upcase = !!value
159
+ # Get the object to use for runtime settings.
160
+ #
161
+ # @return [Object, nil]
162
+ # @api private
163
+ def __runtime_settings__
164
+ @runtime_settings
165
+ end
166
+
167
+ # Explicitly set setting values within a block. This is useful for testing
168
+ # or other situations where you want hard code a specific set of values.
169
+ #
170
+ # @param settings [Hash] The settings to set.
171
+ # @return [Object] The result of the block.
172
+ def override!(settings, &block)
173
+ settings = settings.to_a
174
+ config_name, values = settings.first
175
+ config_name = config_name.to_s
176
+ other_settings = settings[1..-1]
177
+
178
+ unless @configurations.include?(config_name)
179
+ raise ArgumentError.new("Unknown configuration: #{config_name.inspect}")
111
180
  end
112
181
 
113
- # Set the directory to use when loading YAML configuration files. By
114
- # default this is the config directory in the Rails root.
115
- #
116
- # @param value [String, Pathname] The directory to use.
117
- # @return [void]
118
- def yaml_config_directory=(value)
119
- Configuration.yaml_config_directory = value.to_s
182
+ config = send(config_name)
183
+ config.override!(values) do
184
+ if other_settings.empty?
185
+ yield
186
+ else
187
+ override!(other_settings, &block)
188
+ end
120
189
  end
121
190
  end
122
191
 
192
+ # Get the names of all of the configurations that have been added.
193
+ #
194
+ # @return [Array<String>] The names of the configurations.
195
+ # @api private
196
+ def __configuration_names__
197
+ @configurations.keys
198
+ end
199
+
123
200
  private
124
201
 
125
202
  # Load a configuration class.
126
203
  def __load_config__(name, class_name)
127
204
  klass = @configurations[name]
128
205
 
129
- if klass && !Rails.configuration.cache_classes
130
- klass = nil if klass != class_name.constantize
206
+ # Hook for Rails development mode to reload the configuration class.
207
+ if klass && defined?(Rails.configuration.cache_classes) && !Rails.configuration.cache_classes
208
+ klass = class_name if klass != constantize(class_name)
131
209
  end
132
210
 
133
- unless klass
134
- klass = class_name.constantize
211
+ if klass.is_a?(String)
212
+ klass = constantize(class_name)
135
213
  @mutex.synchronize do
136
214
  unless klass < Configuration
137
215
  raise TypeError.new("Configuration class #{class_name} does not inherit from UltraSettings::Configuration")
@@ -142,5 +220,21 @@ module UltraSettings
142
220
 
143
221
  klass.instance
144
222
  end
223
+
224
+ def classify(name)
225
+ # Use the Rails classify method if it is available since it will
226
+ # handle custom inflections.
227
+ if name.respond_to?(:classify)
228
+ name.classify
229
+ else
230
+ name.split("_").map(&:capitalize).join.gsub("/", "::")
231
+ end
232
+ end
233
+
234
+ def constantize(class_name)
235
+ class_name.split("::").reduce(Object) do |mod, name|
236
+ mod.const_get(name)
237
+ end
238
+ end
145
239
  end
146
240
  end
@@ -4,7 +4,7 @@ Gem::Specification.new do |spec|
4
4
  spec.authors = ["Brian Durand"]
5
5
  spec.email = ["bbdurand@gmail.com"]
6
6
 
7
- spec.summary = "Unified configuration for Rails applications that allows for configuration via environment variables, YAML files, and dynamic runtime setting."
7
+ spec.summary = "Unified application configuration that allows for configuration via environment variables, YAML files, and dynamic runtime setting."
8
8
 
9
9
  spec.homepage = "https://github.com/bdurand/ultra_settings"
10
10
  spec.license = "MIT"
@@ -17,6 +17,8 @@ Gem::Specification.new do |spec|
17
17
  Gemfile
18
18
  Gemfile.lock
19
19
  Rakefile
20
+ config.ru
21
+ assets/
20
22
  bin/
21
23
  gemfiles/
22
24
  spec/
@@ -29,8 +31,5 @@ Gem::Specification.new do |spec|
29
31
 
30
32
  spec.required_ruby_version = ">= 2.5"
31
33
 
32
- spec.add_dependency "rails", ">= 5"
33
- spec.add_dependency "super_settings"
34
-
35
34
  spec.add_development_dependency "bundler"
36
35
  end
metadata CHANGED
@@ -1,43 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ultra_settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.rc1
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Durand
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-01 00:00:00.000000000 Z
11
+ date: 2023-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: rails
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '5'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '5'
27
- - !ruby/object:Gem::Dependency
28
- name: super_settings
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
13
  - !ruby/object:Gem::Dependency
42
14
  name: bundler
43
15
  requirement: !ruby/object:Gem::Requirement
@@ -63,9 +35,20 @@ files:
63
35
  - MIT-LICENSE
64
36
  - README.md
65
37
  - VERSION
38
+ - app/application.css
39
+ - app/application.js
40
+ - app/index.html.erb
41
+ - app/layout.css
42
+ - app/layout.html.erb
66
43
  - lib/ultra_settings.rb
44
+ - lib/ultra_settings/coerce.rb
67
45
  - lib/ultra_settings/configuration.rb
68
46
  - lib/ultra_settings/field.rb
47
+ - lib/ultra_settings/rack_app.rb
48
+ - lib/ultra_settings/railtie.rb
49
+ - lib/ultra_settings/version.rb
50
+ - lib/ultra_settings/web_view.rb
51
+ - lib/ultra_settings/yaml_config.rb
69
52
  - ultra_settings.gemspec
70
53
  homepage: https://github.com/bdurand/ultra_settings
71
54
  licenses:
@@ -82,13 +65,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
82
65
  version: '2.5'
83
66
  required_rubygems_version: !ruby/object:Gem::Requirement
84
67
  requirements:
85
- - - ">"
68
+ - - ">="
86
69
  - !ruby/object:Gem::Version
87
- version: 1.3.1
70
+ version: '0'
88
71
  requirements: []
89
- rubygems_version: 3.2.22
72
+ rubygems_version: 3.4.12
90
73
  signing_key:
91
74
  specification_version: 4
92
- summary: Unified configuration for Rails applications that allows for configuration
93
- via environment variables, YAML files, and dynamic runtime setting.
75
+ summary: Unified application configuration that allows for configuration via environment
76
+ variables, YAML files, and dynamic runtime setting.
94
77
  test_files: []