anyway_config 2.4.1 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd660b7594fd85e041d4f00a4abbadef72085256e250df638cd239a3512db58c
4
- data.tar.gz: 94fa4463eaf4bf2d64cddba05ba1fac18e635f62ff89620f8a460c9cf0162535
3
+ metadata.gz: 29fe2702743a353cb8d7d793d59278dea2cfef4075e17204181ffdab9b594274
4
+ data.tar.gz: dd34c9bf703b5b93b4e5c4a1cce3e9ede85af1666d5de484005e13f217d897bd
5
5
  SHA512:
6
- metadata.gz: a1585d343611b50d1ca021dfbd2dfb49f92b8aad835c7f3c48aabeab4d5cc905877db4ef57a86e51b59c53f9b1192f69c7b58af4ec6c1b182447b146e19b4a3b
7
- data.tar.gz: 983a3079303037e6cda97057ad51ef2479000baa1e975d481a7c2f4cd2510605fd36227d3692b1a82f7a85db333a7909e5610515286e1c10adc0aeb0e11f3006
6
+ metadata.gz: 3f1d05c31f97864f64b2ace127636d15535ed18c50f44f11e90949d784df40d437e48d9cd37a0bc2b8ccab0c5b17dd6a25062cbf25669f51e654fb1a5b356b2d
7
+ data.tar.gz: 6a7ceda8a770887e3775d5c75e6d8e5a6afa98f49851926d40d131c4ca0034f60b1739868d142029b35fe44b4bf39205e68d84fe97f65c89c146cbe74a4750aa
data/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 2.5.0 (2023-07-24)
6
+
7
+ - Generators: Add `config_name` to generated classes if name contains underscore. ([@palkan][])
8
+
9
+ - Load Rails extensions even if Rails was loaded after Anyway Config. ([@palkan][])
10
+
11
+ - Fix handling `config.credentials.content_path` provided as String. ([@palkan][])
12
+
13
+ ## 2.4.2 (2023-06-07)
14
+
15
+ - Use ANYWAY_ENV as the current environment if defined. ([@palkan][])
16
+
17
+ It can be used to specify environment for configs independently of Rails environment.
18
+
5
19
  ## 2.4.1 (2023-05-04)
6
20
 
7
21
  - Add custom namespace support via `ejson_namespace` ([@bessey])
data/README.md CHANGED
@@ -367,6 +367,8 @@ development:
367
367
  port: 3000
368
368
  ```
369
369
 
370
+ **NOTE:** You can override the environment name for configuration files via the `ANYWAY_ENV` environment variable or by setting it explicitly in the code: `Anyway::Settings.current_environment = "some_other_env"`.
371
+
370
372
  ### Multi-env configuration
371
373
 
372
374
  _⚡️ This feature will be turned on by default in the future releases. You can turn it on now via `config.anyway_config.future.use :unwrap_known_environments`._
@@ -546,6 +548,14 @@ Alternatively, you can call `rails g anyway:app_config name param1 param2 ...`.
546
548
 
547
549
  **NOTE:** The generated `ApplicationConfig` class uses a singleton pattern along with `delegate_missing_to` to re-use the same instance across the application. However, the delegation can lead to unexpected behaviour and break Anyway Config internals if you have attributes named as `Anyway::Config` class methods. See [#120](https://github.com/palkan/anyway_config/issues/120).
548
550
 
551
+ ### Loading Anyway Config before Rails
552
+
553
+ Anyway Config activates Rails-specific features automatically on the gem load only if Rails has been already required (we check for the `Rails::VERSION` constant presence). However, in some cases you may want to use Anyway Config before Rails initialization (e.g., in `config/puma.rb` when starting a Puma web server).
554
+
555
+ By default, Anyway Config sets up a hook (via TracePoint API) and waits for Rails to be loaded to require the Rails extensions (`require "anyway/rails"`). In case you load Rails after Anyway Config, you will see a warning telling you about that. Note that config classes loaded before Rails are not populated from Rails-specific data sources (e.g., credentials).
556
+
557
+ You can disable the warning by setting `Anyway::Rails.disable_postponed_load_warning = true` in your application. Also, you can disable the _hook_ completely by calling `Anyway::Rails.tracer.disable`.
558
+
549
559
  ## Using with Ruby
550
560
 
551
561
  The default data loading mechanism for non-Rails applications is the following (ordered by priority from low to high):
@@ -559,6 +569,8 @@ If you want to enable this feature you must specify `Anyway::Settings.current_en
559
569
  Anyway::Settings.current_environment = "development"
560
570
  ```
561
571
 
572
+ You can also specify the `ANYWAY_ENV=development` environment variable to set the current environment for configuration.
573
+
562
574
  YAML files should be in this format:
563
575
 
564
576
  ```yml
@@ -299,6 +299,7 @@ module Anyway # :nodoc:
299
299
  "via `config_name :my_config`"
300
300
  end
301
301
 
302
+ # TODO(v3.0): Replace downcase with underscore
302
303
  Regexp.last_match[1].tap(&:downcase!)
303
304
  end
304
305
 
@@ -44,7 +44,7 @@ module Anyway
44
44
  TYPE_TO_CLASS.fetch(type) { defaults[param] ? "Symbol" : "untyped" }
45
45
  when (Array === __m__)
46
46
  "Array[untyped]"
47
- when (((array, type) = nil) || ((__m__.respond_to?(:deconstruct_keys) && (((__m_hash__src__ = __m__.deconstruct_keys(nil)) || true) && (Hash === __m_hash__src__ || Kernel.raise(TypeError, "#deconstruct_keys must return Hash"))) && (__m_hash__ = __m_hash__src__.dup)) && ((__m_hash__.key?(:array) && __m_hash__.key?(:type)) && (((array = __m_hash__.delete(:array)) || true) && (((type = __m_hash__.delete(:type)) || true) && __m_hash__.empty?)))))
47
+ when (((type,) = nil) || ((__m__.respond_to?(:deconstruct_keys) && (((__m_hash__src__ = __m__.deconstruct_keys(nil)) || true) && (Hash === __m_hash__src__ || Kernel.raise(TypeError, "#deconstruct_keys must return Hash"))) && (__m_hash__ = __m_hash__src__.dup)) && ((__m_hash__.key?(:array) && __m_hash__.key?(:type)) && ((((type = __m_hash__.delete(:type)) || true) && __m_hash__.empty?)))))
48
48
  "Array[#{TYPE_TO_CLASS.fetch(type, "untyped")}]"
49
49
  when (Hash === __m__)
50
50
  "Hash[string,untyped]"
@@ -96,8 +96,11 @@ module Anyway
96
96
  end
97
97
  end
98
98
 
99
+ # By default, use ANYWAY_ENV
100
+ self.current_environment = ENV["ANYWAY_ENV"]
101
+
99
102
  # By default, use local files only in development (that's the purpose if the local files)
100
- self.use_local_files = (ENV["RACK_ENV"] == "development" || ENV["RAILS_ENV"] == "development")
103
+ self.use_local_files = (ENV["ANYWAY_ENV"] == "development" || ENV["RACK_ENV"] == "development" || ENV["RAILS_ENV"] == "development")
101
104
 
102
105
  # By default, consider configs are stored in the ./config folder
103
106
  self.default_config_path = ->(name) { "./config/#{name}.yml" }
@@ -299,6 +299,7 @@ module Anyway # :nodoc:
299
299
  "via `config_name :my_config`"
300
300
  end
301
301
 
302
+ # TODO(v3.0): Replace downcase with underscore
302
303
  Regexp.last_match[1].tap(&:downcase!)
303
304
  end
304
305
 
@@ -299,6 +299,7 @@ module Anyway # :nodoc:
299
299
  "via `config_name :my_config`"
300
300
  end
301
301
 
302
+ # TODO(v3.0): Replace downcase with underscore
302
303
  Regexp.last_match[1].tap(&:downcase!)
303
304
  end
304
305
 
data/lib/anyway/config.rb CHANGED
@@ -299,6 +299,7 @@ module Anyway # :nodoc:
299
299
  "via `config_name :my_config`"
300
300
  end
301
301
 
302
+ # TODO(v3.0): Replace downcase with underscore
302
303
  Regexp.last_match[1].tap(&:downcase!)
303
304
  end
304
305
 
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This module is used to detect a Rails application and activate the corresponding plugins
4
+ # when Anyway Config is loaded before Rails (e.g., in config/puma.rb).
5
+ module Anyway
6
+ module Rails
7
+ class << self
8
+ attr_reader :tracer
9
+ attr_accessor :disable_postponed_load_warning
10
+
11
+ private
12
+
13
+ def tracepoint_class_callback(event)
14
+ # Ignore singletons
15
+ return if event.self.singleton_class?
16
+
17
+ # We wait till `rails` has been loaded, which is enough to add a railtie
18
+ # https://github.com/rails/rails/blob/main/railties/lib/rails.rb
19
+ return unless event.self.name == "Rails"
20
+
21
+ # We must check for methods defined in `rails.rb` to distinguish events
22
+ # happening when we open the `Rails` module in other files.
23
+ if defined?(::Rails.env)
24
+ tracer.disable
25
+
26
+ unless disable_postponed_load_warning
27
+ warn "Anyway Config was loaded before Rails. Activating Anyway Config Rails plugins now.\n" \
28
+ "NOTE: Already loaded configs were provisioned without Rails-specific sources."
29
+ end
30
+
31
+ require "anyway/rails"
32
+ end
33
+ end
34
+ end
35
+
36
+ @tracer = TracePoint.new(:class, &method(:tracepoint_class_callback))
37
+ @tracer.enable
38
+ end
39
+ end
@@ -53,7 +53,7 @@ module Anyway
53
53
 
54
54
  def credentials_path
55
55
  if ::Rails.application.config.respond_to?(:credentials)
56
- ::Rails.application.config.credentials.content_path.relative_path_from(::Rails.root).to_s
56
+ ::Rails.root.join(::Rails.application.config.credentials.content_path).relative_path_from(::Rails.root).to_s
57
57
  else
58
58
  "config/credentials.yml.enc"
59
59
  end
@@ -66,7 +66,7 @@ module Anyway
66
66
  end
67
67
 
68
68
  def current_environment
69
- ::Rails.env.to_s
69
+ @current_environment || ::Rails.env.to_s
70
70
  end
71
71
 
72
72
  def app_root
@@ -76,6 +76,7 @@ module Anyway
76
76
 
77
77
  self.default_config_path = ->(name) { ::Rails.root.join("config", "#{name}.yml") }
78
78
  self.known_environments = %w[test development production]
79
+ self.use_local_files ||= ::Rails.env.development?
79
80
  # Don't try read defaults when no key defined
80
81
  self.default_environmental_key = nil
81
82
  end
data/lib/anyway/rbs.rb CHANGED
@@ -44,7 +44,7 @@ module Anyway
44
44
  TYPE_TO_CLASS.fetch(type) { defaults[param] ? "Symbol" : "untyped" }
45
45
  in Array
46
46
  "Array[untyped]"
47
- in array:, type:, **nil
47
+ in array: _, type:, **nil
48
48
  "Array[#{TYPE_TO_CLASS.fetch(type, "untyped")}]"
49
49
  in Hash
50
50
  "Hash[string,untyped]"
@@ -96,8 +96,11 @@ module Anyway
96
96
  end
97
97
  end
98
98
 
99
+ # By default, use ANYWAY_ENV
100
+ self.current_environment = ENV["ANYWAY_ENV"]
101
+
99
102
  # By default, use local files only in development (that's the purpose if the local files)
100
- self.use_local_files = (ENV["RACK_ENV"] == "development" || ENV["RAILS_ENV"] == "development")
103
+ self.use_local_files = (ENV["ANYWAY_ENV"] == "development" || ENV["RACK_ENV"] == "development" || ENV["RAILS_ENV"] == "development")
101
104
 
102
105
  # By default, consider configs are stored in the ./config folder
103
106
  self.default_config_path = ->(name) { "./config/#{name}.yml" }
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "anyway/testing/helpers"
4
4
 
5
- if defined?(RSpec::Core)
5
+ if defined?(RSpec::Core) && RSpec.respond_to?(:configure)
6
6
  RSpec.configure do |config|
7
7
  config.include(
8
8
  Anyway::Testing::Helpers,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Anyway # :nodoc:
4
- VERSION = "2.4.1"
4
+ VERSION = "2.5.0"
5
5
  end
data/lib/anyway_config.rb CHANGED
@@ -45,5 +45,10 @@ module Anyway # :nodoc:
45
45
  end
46
46
  end
47
47
 
48
- require "anyway/rails" if defined?(::Rails::VERSION)
48
+ if defined?(::Rails::VERSION)
49
+ require "anyway/rails"
50
+ else
51
+ require "anyway/rails/autoload"
52
+ end
53
+
49
54
  require "anyway/testing" if ENV["RACK_ENV"] == "test" || ENV["RAILS_ENV"] == "test"
@@ -41,6 +41,11 @@ module Anyway
41
41
  static_config_root
42
42
  end
43
43
  end
44
+
45
+ def needs_config_name?
46
+ raise "No longer needed" if Gem::Version.new(Anyway::VERSION) >= Gem::Version.new("3.0.0")
47
+ file_name.include?("_")
48
+ end
44
49
  end
45
50
  end
46
51
  end
@@ -2,6 +2,9 @@
2
2
 
3
3
  <% module_namespacing do -%>
4
4
  class <%= class_name %>Config < ApplicationConfig
5
+ <%- if needs_config_name? %>
6
+ config_name :<%= file_name %>
7
+ <%- end -%>
5
8
  <%- unless parameters.empty? -%>
6
9
  attr_config <%= parameters.map { |param| ":#{param}" }.join(", ") %>
7
10
  <%- end -%>
data/sig/manifest.yml CHANGED
@@ -4,4 +4,3 @@ dependencies:
4
4
  - name: pathname
5
5
  - name: date
6
6
  - name: time
7
- - name: set
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anyway_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.1
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-04 00:00:00.000000000 Z
11
+ date: 2023-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-next-core
@@ -158,6 +158,7 @@ files:
158
158
  - lib/anyway/option_parser_builder.rb
159
159
  - lib/anyway/optparse_config.rb
160
160
  - lib/anyway/rails.rb
161
+ - lib/anyway/rails/autoload.rb
161
162
  - lib/anyway/rails/config.rb
162
163
  - lib/anyway/rails/loaders.rb
163
164
  - lib/anyway/rails/loaders/credentials.rb