process_settings 0.4.0.pre.3 → 0.4.0.pre.4

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
  SHA1:
3
- metadata.gz: 35f7f59386045aa27ee3134236eed4d9681090b3
4
- data.tar.gz: b9fa6bdfffe65b11b6858b9826a09dfc8de990bd
3
+ metadata.gz: 140a1442c24610ff86f42711e259fa20c478474f
4
+ data.tar.gz: '09b7c3fe96b5fd206d314aa7652671777c44198a'
5
5
  SHA512:
6
- metadata.gz: 173134db7642f5e1622b6c640c12b7c4ef3024bc328bf215fe3cc613b71d47651e068c73504f83af546d2c9cd09085d73d8f3878e86e48a3ec215ac3fa8e50ee
7
- data.tar.gz: 6c910bd0d0372e2c0e41dcd274732dbb3a60e0ac8ad1e9e2998a495c9a438c321c6fc6fba136911ff01416d3e5a6ffbb5db8e71fe091c2800665fcd6d1e6a811
6
+ metadata.gz: 2f922878b0de99a81164c846b148d8f81ac7b73746f93bddc61cda010f550a6b89021cdf89188e5e67b07281117dbfb29894f24bc417e4f0c8d46da1c1d4d811
7
+ data.tar.gz: fe802ec5e18e6fcf4fc0783067ff76e4a5a4fe0e888da9b2bba7f91d8da58d4f873bb76cd965fb50f46afa261ee37a085f6d4256e6fa7f61978704f996551a8f
@@ -2,13 +2,24 @@
2
2
 
3
3
  module ProcessSettings
4
4
  # Module for mixing into `Hash` or other class with `[]` that you want to be able to index
5
- # with a hash path like: hash['app.service_name' => 'frontend']
5
+ # with a hash path like: hash.mine('honeypot', 'answer_odds')
6
6
  module HashPath
7
- def [](key)
8
- if key.is_a?(Hash)
9
- HashPath.hash_at_path(self, key)
10
- else
11
- super
7
+ # returns the value found at the given path
8
+ # if the path is not found:
9
+ # if a block is given, it is called and its value returned (may also raise an exception from the block)
10
+ # else, the not_found_value: is returned
11
+ def mine(*path_array, not_found_value: nil)
12
+ path_array.is_a?(Enumerable) && path_array.size > 0 or raise ArgumentError, "path must be 1 or more keys; got #{path_array.inspect}"
13
+ path_array.reduce(self) do |hash, key|
14
+ if hash.has_key?(key)
15
+ hash[key]
16
+ else
17
+ if block_given?
18
+ break yield
19
+ else
20
+ break not_found_value
21
+ end
22
+ end
12
23
  end
13
24
  end
14
25
 
@@ -31,24 +42,6 @@ module ProcessSettings
31
42
  hash[path]
32
43
  end
33
44
  end
34
-
35
- def set_hash_at_path(hash, path)
36
- path.is_a?(Hash) or raise ArgumentError, "got unexpected non-hash value (#{hash[path]}"
37
- case path.size
38
- when 0
39
- hash
40
- when 1
41
- path_key, path_value = path.first
42
- if path_value.is_a?(Hash)
43
- set_hash_at_path(remaining_hash, remaining_path)
44
- else
45
- hash[path_key] = path_value
46
- end
47
- else
48
- raise ArgumentError, "path may have at most 1 key (got #{path.inspect})"
49
- end
50
- hash
51
- end
52
45
  end
53
46
  end
54
47
  end
@@ -7,6 +7,8 @@ require 'listen'
7
7
  require 'active_support'
8
8
 
9
9
  module ProcessSettings
10
+ class SettingsPathNotFound < StandardError; end
11
+
10
12
  class Monitor
11
13
  attr_reader :file_path, :min_polling_seconds, :logger
12
14
  attr_reader :static_context, :untargeted_settings, :statically_targeted_settings
@@ -26,8 +28,8 @@ module ProcessSettings
26
28
 
27
29
  path = File.dirname(@file_path)
28
30
 
29
- @listener = file_change_notifier.to(path) do |modified, _added, _removed|
30
- if modified.include?(@file_path)
31
+ @listener = file_change_notifier.to(path) do |modified, added, _removed|
32
+ if modified.include?(@file_path) || added.include?(@file_path)
31
33
  @logger.info("ProcessSettings::Monitor file #{@file_path} changed. Reloading.")
32
34
  load_untargeted_settings
33
35
 
@@ -62,20 +64,32 @@ module ProcessSettings
62
64
 
63
65
  # Returns the process settings value at the given `path` using the given `dynamic_context`.
64
66
  # (It is assumed that the static context was already set through static_context=.)
65
- # Returns `nil` if nothing set at the given `path`.
66
- def targeted_value(path, dynamic_context)
67
+ # If nothing set at the given `path`:
68
+ # if required, raises SettingsPathNotFound
69
+ # else returns nil
70
+ def targeted_value(path, dynamic_context:, required: true)
67
71
  # Merging the static context in is necessary to make sure that the static context isn't shifting
68
72
  # this can be rather costly to do every time if the dynamic context is not changing
69
73
  # TODO: Warn in the case where dynamic context was attempting to change a static value
70
74
  # TODO: Cache the last used dynamic context as a potential optimization to avoid unnecessary deep merges
71
75
  full_context = dynamic_context.deep_merge(static_context)
72
- statically_targeted_settings.reduce(nil) do |result, target_and_settings|
76
+ result = statically_targeted_settings.reduce(:not_found) do |latest_result, target_and_settings|
73
77
  # find last value from matching targets
74
78
  if target_and_settings.target.target_key_matches?(full_context)
75
- unless (value = HashPath.hash_at_path(target_and_settings.process_settings, path)).nil?
76
- result = value
79
+ if (value = target_and_settings.process_settings.json_doc.mine(*path, not_found_value: :not_found)) != :not_found
80
+ latest_result = value
77
81
  end
78
82
  end
83
+ latest_result
84
+ end
85
+
86
+ if result == :not_found
87
+ if required
88
+ raise SettingsPathNotFound, "no settings found for path #{path.inspect}"
89
+ else
90
+ nil
91
+ end
92
+ else
79
93
  result
80
94
  end
81
95
  end
@@ -21,9 +21,5 @@ module ProcessSettings
21
21
  def eql?(rhs)
22
22
  self == rhs
23
23
  end
24
-
25
- def [](key)
26
- @json_doc[key]
27
- end
28
24
  end
29
25
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ProcessSettings
4
- VERSION = '0.4.0.pre.3'
4
+ VERSION = '0.4.0.pre.4'
5
5
  end
@@ -7,8 +7,8 @@ require 'process_settings/monitor'
7
7
 
8
8
  module ProcessSettings
9
9
  class << self
10
- def [](value, dynamic_context = {})
11
- Monitor.instance.targeted_value(value, dynamic_context)
10
+ def [](value, dynamic_context: {}, required: true)
11
+ Monitor.instance.targeted_value(value, dynamic_context: dynamic_context, required: required)
12
12
  end
13
13
  end
14
14
  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.4.0.pre.3
4
+ version: 0.4.0.pre.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Invoca