process_settings 0.13.2 → 0.15.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: 3825ac379576930a892de053c60a8a5ea108cf18ca231c36afb74d29e023e873
4
- data.tar.gz: 9e06cf4895b856fa801f868a5e600023ab27cbbee2a730f8d2c776de6b14ea62
3
+ metadata.gz: 17af8e8b00a219e790b36fc20282e0c4c7d7c04cfd7f911ea636f056b675cf70
4
+ data.tar.gz: 198665150f57b95634698ab3474dd587dbefbc48ecf41d10084a6f872a0a3949
5
5
  SHA512:
6
- metadata.gz: 38a7e49c5782b72c334f9029388ffb06aa94cf3fd7dffc73a7750f102445c21fb8788df60d37af53e68f1e58956ef429cd7a3a442e117095eea74bd89bc2340d
7
- data.tar.gz: ac57357cd9873b9a8f42b98046b5ebd942e9058083d0b71711879ea73c917671967ffbc34aa3be77dc3b8b844d6f966a3b13c88860ccc2a7e8a659f1eecad117
6
+ metadata.gz: ea2bf7c35886a10807a3b8ab171133776c1381371b628ac08cbe413a2cbc8d4bc8c600142fc44c0734fb1e2dc6bb2143be40c68c5b99217404fa6a085ccf2326
7
+ data.tar.gz: b92dbd32b2935e7d3ba4b8b95f33dc032e018a273505f7daaba4126d1ef7f132fe40e79535780c45a3283df9daa3c006ce1312c2cf2c2eb472b7b123f09fa10f
data/README.md CHANGED
@@ -169,8 +169,8 @@ The settings YAML files are always combined in alphabetical order by file path.
169
169
 
170
170
  ### Testing
171
171
  For testing, it is often necessary to set a specific override hash for the process_settings values to use in
172
- that use case. The `ProcessSettings::Testing::Helpers` module is provided for this purpose. It can be used to
173
- override a specific hash of process settings, while leaving the rest intact, and resetting back to the defaults
172
+ that use case. The `ProcessSettings::Testing::RSpec::Helpers` and `ProcessSettings::Testing::Minitest::Helpers` modules are provided for this purpose.
173
+ They can be used to override a specific hash of process settings, while leaving the rest intact, and resetting back to the defaults
174
174
  after the test case is over. Here are some examples using various testing frameworks:
175
175
 
176
176
  #### RSpec
@@ -181,7 +181,7 @@ require 'process_settings/testing/helpers'
181
181
  RSpec.configure do |config|
182
182
  # ...
183
183
 
184
- include ProcessSettings::Testing::Helpers
184
+ include ProcessSettings::Testing::RSpec::Helpers
185
185
 
186
186
  # Note: the include above will automatically register a global after block that will reset process_settings to their initial values.
187
187
  # ...
@@ -206,7 +206,7 @@ end
206
206
  require 'process_settings/testing/helpers'
207
207
 
208
208
  context SomeClass do
209
- include ProcessSettings::Testing::Helpers
209
+ include ProcessSettings::Testing::Minitest::Helpers
210
210
 
211
211
  # Note: the include above will automatically register a teardown block that will reset process_settings to their initial values.
212
212
 
@@ -77,11 +77,11 @@ end
77
77
 
78
78
  MINIMUM_LIBYAML_VERSION = '0.2.5' # So that null (nil) values don't have trailing spaces.
79
79
 
80
- def check_libyaml_version
80
+ def warn_if_old_libyaml_version
81
81
  if Gem::Version.new(Psych::LIBYAML_VERSION) < Gem::Version.new(MINIMUM_LIBYAML_VERSION)
82
82
  warn <<~EOS
83
83
 
84
- #{PROGRAM_NAME} error: libyaml version #{Psych::LIBYAML_VERSION} must be at least #{MINIMUM_LIBYAML_VERSION}. Try:
84
+ #{PROGRAM_NAME} warning: libyaml version #{Psych::LIBYAML_VERSION} is out of date; it should be at least #{MINIMUM_LIBYAML_VERSION}. On a Mac, try:
85
85
 
86
86
  brew update && brew upgrade libyaml
87
87
 
@@ -89,8 +89,6 @@ def check_libyaml_version
89
89
 
90
90
  gem install psych -- --enable-bundled-libyaml
91
91
  EOS
92
-
93
- exit(1)
94
92
  end
95
93
  end
96
94
 
@@ -101,14 +99,14 @@ end
101
99
 
102
100
  options = parse_options(ARGV.dup)
103
101
 
104
- check_libyaml_version
102
+ warn_if_old_libyaml_version
105
103
 
106
104
  combined_settings = read_and_combine_settings(Pathname.new(options.root_folder) + SETTINGS_FOLDER)
107
105
 
108
106
  version_number = options.version || default_version_number(options.initial_filename)
109
107
  combined_settings << end_marker(version_number)
110
108
 
111
- yaml = combined_settings.to_yaml
109
+ yaml = combined_settings.to_yaml.gsub(/: $/, ':') # libyaml before 0.2.5 wrote trailing space for nil
112
110
  yaml_with_warning_comment = add_warning_comment(yaml, options.root_folder, PROGRAM_NAME, SETTINGS_FOLDER)
113
111
 
114
112
  output_filename = options.output_filename
@@ -11,14 +11,16 @@ module ProcessSettings
11
11
  OnChangeDeprecation = ActiveSupport::Deprecation.new('1.0', 'ProcessSettings::Monitor')
12
12
 
13
13
  class AbstractMonitor
14
+
14
15
  attr_reader :min_polling_seconds, :logger
15
- attr_reader :static_context, :statically_targeted_settings
16
+ attr_reader :static_context, :statically_targeted_settings, :full_context_cache
16
17
 
17
18
  def initialize(logger:)
18
19
  @logger = logger or raise ArgumentError, "logger must be not be nil"
19
20
  @on_change_callbacks = []
20
21
  @when_updated_blocks = Set.new
21
22
  @static_context = {}
23
+ @full_context_cache = {}
22
24
  end
23
25
 
24
26
  # This is the main entry point for looking up settings on the Monitor instance.
@@ -94,10 +96,20 @@ module ProcessSettings
94
96
  def targeted_value(*path, dynamic_context:, required: true)
95
97
  # Merging the static context in is necessary to make sure that the static context isn't shifting
96
98
  # this can be rather costly to do every time if the dynamic context is not changing
97
- # TODO: Warn in the case where dynamic context was attempting to change a static value
98
- # TODO: Cache the last used dynamic context as a potential optimization to avoid unnecessary deep merges
99
- # TECH-4402 was created to address these todos
100
- full_context = dynamic_context.deep_merge(static_context)
99
+
100
+ # Warn in the case where dynamic context was attempting to change a static value
101
+ changes = dynamic_context.each_with_object({}) do |(key, dynamic_value), result|
102
+ if static_context.has_key?(key)
103
+ static_value = static_context[key]
104
+ if static_value != dynamic_value
105
+ result[key] = [static_value, dynamic_value]
106
+ end
107
+ end
108
+ end
109
+
110
+ changes.empty? or warn("WARNING: static context overwritten by dynamic!\n#{changes.inspect}")
111
+
112
+ full_context = full_context_from_cache(dynamic_context)
101
113
  result = statically_targeted_settings.reduce(:not_found) do |latest_result, target_and_settings|
102
114
  # find last value from matching targets
103
115
  if target_and_settings.target.target_key_matches?(full_context)
@@ -119,6 +131,22 @@ module ProcessSettings
119
131
  end
120
132
  end
121
133
 
134
+ def full_context_from_cache(dynamic_context)
135
+ if (full_context = full_context_cache[dynamic_context])
136
+ logger.info("cache hit ...")
137
+ full_context
138
+ else
139
+ logger.info("cache miss ...")
140
+ dynamic_context.deep_merge(static_context).tap do |full_context|
141
+ if full_context_cache.size <= 1000
142
+ full_context_cache[dynamic_context] = full_context
143
+ else
144
+ logger.info("cache limit reached ...")
145
+ end
146
+ end
147
+ end
148
+ end
149
+
122
150
  private
123
151
 
124
152
  class << self
@@ -8,48 +8,66 @@ require 'process_settings/testing/monitor'
8
8
 
9
9
  module ProcessSettings
10
10
  module Testing
11
- module Helpers
12
- class << self
13
- def included(including_klass)
14
- after_method =
15
- if including_klass.respond_to?(:teardown)
16
- :teardown
17
- else
18
- :after
19
- end
11
+ module Base
12
+ module Helpers
13
+ # Adds the given settings_hash as an override at the end of the process_settings array, with default targeting (true).
14
+ # Therefore this will override these settings while leaving others alone.
15
+ #
16
+ # @param [Hash] settings_hash
17
+ #
18
+ # @return none
19
+ def stub_process_settings(settings_hash)
20
+ new_target_and_settings = ProcessSettings::TargetAndSettings.new(
21
+ '<test_override>',
22
+ Target::true_target,
23
+ ProcessSettings::Settings.new(settings_hash.deep_stringify_keys)
24
+ )
20
25
 
21
- including_klass.send(after_method) do
22
- ProcessSettings.instance = initial_instance
23
- end
26
+ new_process_settings = [
27
+ *initial_instance.statically_targeted_settings,
28
+ new_target_and_settings
29
+ ]
30
+
31
+ ProcessSettings.instance = ProcessSettings::Testing::Monitor.new(
32
+ new_process_settings,
33
+ logger: initial_instance.logger
34
+ )
35
+ end
36
+
37
+ def initial_instance
38
+ @initial_instance ||= ProcessSettings.instance
24
39
  end
25
40
  end
26
- # Adds the given settings_hash as an override at the end of the process_settings array, with default targeting (true).
27
- # Therefore this will override these settings while leaving others alone.
28
- #
29
- # @param [Hash] settings_hash
30
- #
31
- # @return none
32
- def stub_process_settings(settings_hash)
33
- new_target_and_settings = ProcessSettings::TargetAndSettings.new(
34
- '<test_override>',
35
- Target::true_target,
36
- ProcessSettings::Settings.new(settings_hash.deep_stringify_keys)
37
- )
38
-
39
- new_process_settings = [
40
- *initial_instance.statically_targeted_settings,
41
- new_target_and_settings
42
- ]
43
-
44
- ProcessSettings.instance = ProcessSettings::Testing::Monitor.new(
45
- new_process_settings,
46
- logger: initial_instance.logger
47
- )
41
+ end
42
+
43
+ module RSpec
44
+ module Helpers
45
+ include Base::Helpers
46
+
47
+ class << self
48
+ def included(including_klass)
49
+ including_klass.after do
50
+ ProcessSettings.instance = initial_instance
51
+ end
52
+ end
53
+ end
48
54
  end
55
+ end
56
+
57
+ module Minitest
58
+ module Helpers
59
+ include Base::Helpers
49
60
 
50
- def initial_instance
51
- @initial_instance ||= ProcessSettings.instance
61
+ class << self
62
+ def included(including_klass)
63
+ including_klass.define_method(:teardown) do
64
+ ProcessSettings.instance = initial_instance
65
+ end
66
+ end
67
+ end
52
68
  end
53
69
  end
70
+
71
+ Helpers = RSpec::Helpers # for backward-compatibility
54
72
  end
55
73
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ProcessSettings
4
- VERSION = '0.13.2'
4
+ VERSION = '0.15.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: process_settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.2
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Invoca