process_settings 0.4.0.pre.3 → 0.4.0.pre.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/process_settings/hash_path.rb +17 -24
- data/lib/process_settings/monitor.rb +21 -7
- data/lib/process_settings/settings.rb +0 -4
- data/lib/process_settings/version.rb +1 -1
- data/lib/process_settings.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 140a1442c24610ff86f42711e259fa20c478474f
|
4
|
+
data.tar.gz: '09b7c3fe96b5fd206d314aa7652671777c44198a'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
5
|
+
# with a hash path like: hash.mine('honeypot', 'answer_odds')
|
6
6
|
module HashPath
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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,
|
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
|
-
#
|
66
|
-
|
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(
|
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
|
-
|
76
|
-
|
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
|
data/lib/process_settings.rb
CHANGED
@@ -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
|