app_configurable 0.1.4 → 0.1.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 044562b2da9b8df939abe454b140aab3d3389a893dc2dd31f278d9e9eae8495b
4
- data.tar.gz: 932b8ae2e26fb5449a52c815e578689ea24a88578c11df79165d42edd5a2801a
3
+ metadata.gz: 9597da9c6845968ffaaa9e69cc09db480e7c12f3a020b49ed4bb9fc69d69c480
4
+ data.tar.gz: 5e8119d5d21019a74827115136fadf967f4617ad43b296f4d934a3c0382c939d
5
5
  SHA512:
6
- metadata.gz: 389a4ff576b8a3da9207ca41570becd67f3b5af38fb0e046c80468102ccd30a1d925f7a8817044f2ebaa88e900df26f5378972d5f92bc09bd0ac6eded9df9148
7
- data.tar.gz: 190ec85b5ce0a351ae45d343d685a438b2b681f72746506c75ab01b5a373f5ffb493aa7d97706a0c1cabdd21878769ee4224880cd3d0afd7b1a1e99a8adacf94
6
+ metadata.gz: 9db35c02167a567a58a8d2ede828fd7a9af07048d7ec037fb0b7626df89c2ce0f60058a05653731cdf927b86d646284d90f9497748c4d31bee95f424b7df64ea
7
+ data.tar.gz: 9cca5bc92a16a2cc76b05d31d01a9b5e959bb5a5eadc68f87ebcba259f729cb2b7b386696ce950155a17fb6f3d11136b4eb2c79941df5ab6ea4117649f549511
data/Gemfile.lock CHANGED
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- app_configurable (0.1.4)
4
+ app_configurable (0.1.6)
5
+ activesupport
5
6
  dotenv
6
7
 
7
8
  GEM
data/README.md CHANGED
@@ -29,13 +29,17 @@ App Configurable is a simple Ruby gem that provides a centralized and consistent
29
29
 
30
30
  Define an initializer `config/initializers/_app_config.rb`:
31
31
  ```ruby
32
- AppConfigurable.load(%w[./config/app_config.rb])
32
+ AppConfigurable.load_configs(%w[./config/app_config.rb])
33
33
  ```
34
34
  OR
35
35
  ```ruby
36
- AppConfigurable.load(%w[./config/app_config.rb], raise_on_missing: true) # Fails on startup, reporting missing configs.
36
+ AppConfigurable.load_configs(%w[./config/app_config.rb], rails_env: 'production') # Set's environment to `production`.
37
37
  ```
38
- *Alternatively, you could define your configs under the autoloading path if failing on demand is acceptable.*
38
+ OR
39
+ ```ruby
40
+ AppConfigurable.load_configs(%w[./config/app_config.rb], raise_on_missing: true) # Fails on startup, reporting missing configs.
41
+ ```
42
+ *Alternatively, you could define your configs under the autoloading path if failing on demand is acceptable.*
39
43
 
40
44
  4. Access your configurations:
41
45
  ```ruby
@@ -97,7 +101,16 @@ Example: `entry :rails_serve_static_files`.
97
101
  3. Return `some_super_dummy_#{namespaced_attribute_name}`, **ignoring the specified `default` attribute**.
98
102
 
99
103
  ### Setting the Environment
100
- You can set the environment per namespace like so:
104
+ By default your environment will is set from `Rails.env`.
105
+
106
+ #### Globally, when initialising configs:
107
+
108
+ `AppConfigurable.load_configs(%w[./config/app_config.rb], rails_env: 'production')`.
109
+
110
+ *Might be useful to disable error reporting for assets precompilation or any rake tasks which don't require secrets but loading full rails environment.*
111
+
112
+
113
+ #### Per namespace:
101
114
 
102
115
  Given namespace `AppConfig::MyNiceConfigClass` and `.env.staging`:
103
116
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AppConfigurable
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.6'
5
5
  end
@@ -27,14 +27,21 @@ module AppConfigurable
27
27
 
28
28
  # @param paths [Array] List of paths to load.
29
29
  # @param raise_on_missing [Boolean] Raise an error if required variables are missing, default is `false`.
30
+ # @param rails_env [String/] Rails environment.
30
31
  # @return [void]
31
- def self.load_configs(paths = [], raise_on_missing: false)
32
- paths.each do |path|
32
+ def self.load_configs(paths = [], raise_on_missing: false, rails_env: Rails.env) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
33
+ rails_env = ActiveSupport::StringInquirer.new(rails_env) unless rails_env.is_a?(ActiveSupport::StringInquirer)
34
+
35
+ paths.map do |path|
33
36
  absolute_path = Dir.pwd + "/#{path}"
34
37
 
35
38
  File.directory?(absolute_path) ? Dir["#{absolute_path}/**/*.rb"].each { |f| require f } : require(absolute_path)
36
39
  end
37
40
 
41
+ available_config_attributes.collect(&:receiver).uniq.each do |klass|
42
+ klass.instance.send(:rails_env=, rails_env, swallow_errors: true)
43
+ end
44
+
38
45
  raise_on_missing && missing_required_vars.any? && raise(Error::RequiredVarMissing, missing_required_vars.join(', '))
39
46
  end
40
47
 
@@ -150,14 +157,15 @@ module AppConfigurable
150
157
  @rails_env ||= submodule_env || ::Rails.env
151
158
  end
152
159
 
153
- def rails_env=(value)
160
+ def rails_env=(value, swallow_errors: false)
154
161
  @rails_env =
155
162
  begin
156
163
  wrapper_klass = ActiveSupport::StringInquirer
157
- value.is_a?(wrapper_klass) ? value : wrapper_klass.new(value)
164
+ submodule_env || (value.is_a?(wrapper_klass) ? value : wrapper_klass.new(value))
158
165
  end
166
+
159
167
  recalculate_env
160
- recalculate_values
168
+ recalculate_values(swallow_errors:)
161
169
  end
162
170
 
163
171
  # @return [ActiveSupport::StringInquirer/nil]
@@ -246,12 +254,19 @@ module AppConfigurable
246
254
  send(:env)
247
255
  end
248
256
 
249
- # Recalculates the values of all the attributes.
257
+ # Recalculates values of all attributes.
258
+ # @param swallow_errors [Boolean] Don't raise an exception in case of missing attributes, default is `false`.
250
259
  # @return [void]
251
- def recalculate_values
260
+ def recalculate_values(swallow_errors: false)
252
261
  self.class.config_attributes.each do |a|
262
+ instance_variable_get(:"@#{a}")
253
263
  remove_instance_variable(:"@#{a}") if instance_variable_defined?(:"@#{a}")
254
- send(a)
264
+
265
+ begin
266
+ send(a)
267
+ rescue Error::RequiredVarMissing
268
+ swallow_errors ? next : raise
269
+ end
255
270
  end
256
271
  end
257
272
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app_configurable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmytro Pasichnyk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-22 00:00:00.000000000 Z
11
+ date: 2024-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: dotenv
15
29
  requirement: !ruby/object:Gem::Requirement