chef-utils 16.10.17 → 17.10.19

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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +201 -201
  3. data/Rakefile +15 -15
  4. data/chef-utils.gemspec +50 -46
  5. data/lib/chef-utils/dist.rb +151 -98
  6. data/lib/chef-utils/dsl/architecture.rb +150 -150
  7. data/lib/chef-utils/dsl/cloud.rb +155 -144
  8. data/lib/chef-utils/dsl/default_paths.rb +60 -60
  9. data/lib/chef-utils/dsl/introspection.rb +134 -123
  10. data/lib/chef-utils/dsl/os.rb +58 -58
  11. data/lib/chef-utils/dsl/path_sanity.rb +39 -39
  12. data/lib/chef-utils/dsl/platform.rb +387 -372
  13. data/lib/chef-utils/dsl/platform_family.rb +355 -344
  14. data/lib/chef-utils/dsl/platform_version.rb +41 -41
  15. data/lib/chef-utils/dsl/service.rb +112 -112
  16. data/lib/chef-utils/dsl/train_helpers.rb +87 -87
  17. data/lib/chef-utils/dsl/virtualization.rb +272 -250
  18. data/lib/chef-utils/dsl/which.rb +123 -123
  19. data/lib/chef-utils/dsl/windows.rb +86 -86
  20. data/lib/chef-utils/internal.rb +114 -114
  21. data/lib/chef-utils/mash.rb +263 -240
  22. data/lib/chef-utils/parallel_map.rb +131 -0
  23. data/lib/chef-utils/version.rb +20 -20
  24. data/lib/chef-utils/version_string.rb +160 -160
  25. data/lib/chef-utils.rb +53 -53
  26. data/spec/spec_helper.rb +100 -100
  27. data/spec/unit/dsl/architecture_spec.rb +151 -151
  28. data/spec/unit/dsl/cloud_spec.rb +93 -89
  29. data/spec/unit/dsl/dsl_spec.rb +34 -34
  30. data/spec/unit/dsl/introspection_spec.rb +201 -189
  31. data/spec/unit/dsl/os_spec.rb +175 -175
  32. data/spec/unit/dsl/path_sanity_spec.rb +86 -86
  33. data/spec/unit/dsl/platform_family_spec.rb +235 -223
  34. data/spec/unit/dsl/platform_spec.rb +252 -238
  35. data/spec/unit/dsl/service_spec.rb +117 -117
  36. data/spec/unit/dsl/virtualization_spec.rb +75 -75
  37. data/spec/unit/dsl/which_spec.rb +171 -171
  38. data/spec/unit/dsl/windows_spec.rb +84 -84
  39. data/spec/unit/mash_spec.rb +51 -51
  40. data/spec/unit/parallel_map_spec.rb +156 -0
  41. metadata +26 -10
@@ -1,240 +1,263 @@
1
- # frozen_string_literal: true
2
- # Copyright 2009-2016, Dan Kubb
3
-
4
- # Permission is hereby granted, free of charge, to any person obtaining
5
- # a copy of this software and associated documentation files (the
6
- # "Software"), to deal in the Software without restriction, including
7
- # without limitation the rights to use, copy, modify, merge, publish,
8
- # distribute, sublicense, and/or sell copies of the Software, and to
9
- # permit persons to whom the Software is furnished to do so, subject to
10
- # the following conditions:
11
-
12
- # The above copyright notice and this permission notice shall be
13
- # included in all copies or substantial portions of the Software.
14
-
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
-
23
- # ---
24
- # ---
25
-
26
- # Some portions of blank.rb and mash.rb are verbatim copies of software
27
- # licensed under the MIT license. That license is included below:
28
-
29
- # Copyright 2005-2016, David Heinemeier Hansson
30
-
31
- # Permission is hereby granted, free of charge, to any person obtaining
32
- # a copy of this software and associated documentation files (the
33
- # "Software"), to deal in the Software without restriction, including
34
- # without limitation the rights to use, copy, modify, merge, publish,
35
- # distribute, sublicense, and/or sell copies of the Software, and to
36
- # permit persons to whom the Software is furnished to do so, subject to
37
- # the following conditions:
38
-
39
- # The above copyright notice and this permission notice shall be
40
- # included in all copies or substantial portions of the Software.
41
-
42
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
43
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
45
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
46
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
47
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
48
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49
-
50
- # This class has dubious semantics and we only have it so that people can write
51
- # params[:key] instead of params['key'].
52
- module ChefUtils
53
- class Mash < Hash
54
-
55
- # @param constructor<Object>
56
- # The default value for the mash. Defaults to an empty hash.
57
- #
58
- # @details [Alternatives]
59
- # If constructor is a Hash, a new mash will be created based on the keys of
60
- # the hash and no default value will be set.
61
- def initialize(constructor = {})
62
- if constructor.is_a?(Hash)
63
- super()
64
- update(constructor)
65
- else
66
- super(constructor)
67
- end
68
- end
69
-
70
- # @param orig<Object> Mash being copied
71
- #
72
- # @return [Object] A new copied Mash
73
- def initialize_copy(orig)
74
- super
75
- # Handle nested values
76
- each do |k, v|
77
- if v.is_a?(Mash) || v.is_a?(Array)
78
- self[k] = v.dup
79
- end
80
- end
81
- self
82
- end
83
-
84
- # @param key<Object> The default value for the mash. Defaults to nil.
85
- #
86
- # @details [Alternatives]
87
- # If key is a Symbol and it is a key in the mash, then the default value will
88
- # be set to the value matching the key.
89
- def default(key = nil)
90
- if key.is_a?(Symbol) && include?(key = key.to_s)
91
- self[key]
92
- else
93
- super
94
- end
95
- end
96
-
97
- unless method_defined?(:regular_writer)
98
- alias_method :regular_writer, :[]=
99
- end
100
-
101
- unless method_defined?(:regular_update)
102
- alias_method :regular_update, :update
103
- end
104
-
105
- # @param key<Object> The key to set.
106
- # @param value<Object>
107
- # The value to set the key to.
108
- #
109
- # @see Mash#convert_key
110
- # @see Mash#convert_value
111
- def []=(key, value)
112
- regular_writer(convert_key(key), convert_value(value))
113
- end
114
-
115
- # internal API for use by Chef's deep merge cache
116
- # @api private
117
- def internal_set(key, value)
118
- regular_writer(key, convert_value(value))
119
- end
120
-
121
- # @param other_hash<Hash>
122
- # A hash to update values in the mash with. The keys and the values will be
123
- # converted to Mash format.
124
- #
125
- # @return [Mash] The updated mash.
126
- def update(other_hash)
127
- other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
128
- self
129
- end
130
-
131
- alias_method :merge!, :update
132
-
133
- # @param key<Object> The key to check for. This will be run through convert_key.
134
- #
135
- # @return [Boolean] True if the key exists in the mash.
136
- def key?(key)
137
- super(convert_key(key))
138
- end
139
-
140
- # def include? def has_key? def member?
141
- alias_method :include?, :key?
142
- alias_method :has_key?, :key?
143
- alias_method :member?, :key?
144
-
145
- # @param key<Object> The key to fetch. This will be run through convert_key.
146
- # @param *extras<Array> Default value.
147
- #
148
- # @return [Object] The value at key or the default value.
149
- def fetch(key, *extras)
150
- super(convert_key(key), *extras)
151
- end
152
-
153
- # @param *indices<Array>
154
- # The keys to retrieve values for. These will be run through +convert_key+.
155
- #
156
- # @return [Array] The values at each of the provided keys
157
- def values_at(*indices)
158
- indices.collect { |key| self[convert_key(key)] }
159
- end
160
-
161
- # @param hash<Hash> The hash to merge with the mash.
162
- #
163
- # @return [Mash] A new mash with the hash values merged in.
164
- def merge(hash)
165
- dup.update(hash)
166
- end
167
-
168
- # @param key<Object>
169
- # The key to delete from the mash.\
170
- def delete(key)
171
- super(convert_key(key))
172
- end
173
-
174
- # @param *rejected<Array[(String, Symbol)] The mash keys to exclude.
175
- #
176
- # @return [Mash] A new mash without the selected keys.
177
- #
178
- # @example
179
- # { :one => 1, :two => 2, :three => 3 }.except(:one)
180
- # #=> { "two" => 2, "three" => 3 }
181
- def except(*keys)
182
- super(*keys.map { |k| convert_key(k) })
183
- end
184
-
185
- # Used to provide the same interface as Hash.
186
- #
187
- # @return [Mash] This mash unchanged.
188
- def stringify_keys!; self end
189
-
190
- # @return [Hash] The mash as a Hash with symbolized keys.
191
- def symbolize_keys
192
- h = Hash.new(default)
193
- each { |key, val| h[key.to_sym] = val }
194
- h
195
- end
196
-
197
- # @return [Hash] The mash as a Hash with string keys.
198
- def to_hash
199
- Hash.new(default).merge(self)
200
- end
201
-
202
- # @return [Mash] Convert a Hash into a Mash
203
- # The input Hash's default value is maintained
204
- def self.from_hash(hash)
205
- mash = Mash.new(hash)
206
- mash.default = hash.default
207
- mash
208
- end
209
-
210
- protected
211
-
212
- # @param key<Object> The key to convert.
213
- #
214
- # @param [Object]
215
- # The converted key. If the key was a symbol, it will be converted to a
216
- # string.
217
- #
218
- # @api private
219
- def convert_key(key)
220
- key.is_a?(Symbol) ? key.to_s : key
221
- end
222
-
223
- # @param value<Object> The value to convert.
224
- #
225
- # @return [Object]
226
- # The converted value. A Hash or an Array of hashes, will be converted to
227
- # their Mash equivalents.
228
- #
229
- # @api private
230
- def convert_value(value)
231
- if value.class == Hash
232
- Mash.from_hash(value)
233
- elsif value.is_a?(Array)
234
- value.collect { |e| convert_value(e) }
235
- else
236
- value
237
- end
238
- end
239
- end
240
- end
1
+ # frozen_string_literal: true
2
+ # Copyright 2009-2016, Dan Kubb
3
+
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ # ---
24
+ # ---
25
+
26
+ # Some portions of blank.rb and mash.rb are verbatim copies of software
27
+ # licensed under the MIT license. That license is included below:
28
+
29
+ # Copyright 2005-2016, David Heinemeier Hansson
30
+
31
+ # Permission is hereby granted, free of charge, to any person obtaining
32
+ # a copy of this software and associated documentation files (the
33
+ # "Software"), to deal in the Software without restriction, including
34
+ # without limitation the rights to use, copy, modify, merge, publish,
35
+ # distribute, sublicense, and/or sell copies of the Software, and to
36
+ # permit persons to whom the Software is furnished to do so, subject to
37
+ # the following conditions:
38
+
39
+ # The above copyright notice and this permission notice shall be
40
+ # included in all copies or substantial portions of the Software.
41
+
42
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
43
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
45
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
46
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
47
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
48
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49
+
50
+ # This class has dubious semantics and we only have it so that people can write
51
+ # params[:key] instead of params['key'].
52
+ module ChefUtils
53
+ class Mash < Hash
54
+
55
+ # @param constructor<Object>
56
+ # The default value for the mash. Defaults to an empty hash.
57
+ #
58
+ # @details [Alternatives]
59
+ # If constructor is a Hash, a new mash will be created based on the keys of
60
+ # the hash and no default value will be set.
61
+ def initialize(constructor = {})
62
+ if constructor.is_a?(Hash)
63
+ super()
64
+ update(constructor)
65
+ else
66
+ super(constructor)
67
+ end
68
+ end
69
+
70
+ # @param orig<Object> Mash being copied
71
+ #
72
+ # @return [Object] A new copied Mash
73
+ def initialize_copy(orig)
74
+ super
75
+ # Handle nested values
76
+ each do |k, v|
77
+ if v.is_a?(Mash) || v.is_a?(Array)
78
+ self[k] = v.dup
79
+ end
80
+ end
81
+ self
82
+ end
83
+
84
+ # @param key<Object> The default value for the mash. Defaults to nil.
85
+ #
86
+ # @details [Alternatives]
87
+ # If key is a Symbol and it is a key in the mash, then the default value will
88
+ # be set to the value matching the key.
89
+ def default(key = nil)
90
+ if key.is_a?(Symbol) && include?(key = key.to_s)
91
+ self[key]
92
+ else
93
+ super
94
+ end
95
+ end
96
+
97
+ unless method_defined?(:regular_reader)
98
+ alias_method :regular_reader, :[]
99
+ end
100
+
101
+ unless method_defined?(:regular_writer)
102
+ alias_method :regular_writer, :[]=
103
+ end
104
+
105
+ unless method_defined?(:regular_update)
106
+ alias_method :regular_update, :update
107
+ end
108
+
109
+ unless method_defined?(:regular_clear)
110
+ alias_method :regular_clear, :clear
111
+ end
112
+
113
+ unless method_defined?(:regular_delete)
114
+ alias_method :regular_delete, :delete
115
+ end
116
+
117
+ # @param key<Object> The key to get.
118
+ def [](key)
119
+ regular_reader(key)
120
+ end
121
+
122
+ # @param key<Object> The key to set.
123
+ # @param value<Object>
124
+ # The value to set the key to.
125
+ #
126
+ # @see Mash#convert_key
127
+ # @see Mash#convert_value
128
+ def []=(key, value)
129
+ regular_writer(convert_key(key), convert_value(value))
130
+ end
131
+
132
+ # internal API for use by Chef's deep merge cache
133
+ # @api private
134
+ def internal_get(key)
135
+ regular_reader(key)
136
+ end
137
+
138
+ # internal API for use by Chef's deep merge cache
139
+ # @api private
140
+ def internal_set(key, value)
141
+ regular_writer(key, convert_value(value))
142
+ end
143
+
144
+ # @param other_hash<Hash>
145
+ # A hash to update values in the mash with. The keys and the values will be
146
+ # converted to Mash format.
147
+ #
148
+ # @return [Mash] The updated mash.
149
+ def update(other_hash)
150
+ other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
151
+ self
152
+ end
153
+
154
+ alias_method :merge!, :update
155
+
156
+ # @param key<Object> The key to check for. This will be run through convert_key.
157
+ #
158
+ # @return [Boolean] True if the key exists in the mash.
159
+ def key?(key)
160
+ super(convert_key(key))
161
+ end
162
+
163
+ # def include? def has_key? def member?
164
+ alias_method :include?, :key?
165
+ alias_method :has_key?, :key?
166
+ alias_method :member?, :key?
167
+
168
+ # @param key<Object> The key to fetch. This will be run through convert_key.
169
+ # @param *extras<Array> Default value.
170
+ #
171
+ # @return [Object] The value at key or the default value.
172
+ def fetch(key, *extras)
173
+ super(convert_key(key), *extras)
174
+ end
175
+
176
+ # @param *indices<Array>
177
+ # The keys to retrieve values for. These will be run through +convert_key+.
178
+ #
179
+ # @return [Array] The values at each of the provided keys
180
+ def values_at(*indices)
181
+ indices.collect { |key| self[convert_key(key)] }
182
+ end
183
+
184
+ # @param hash<Hash> The hash to merge with the mash.
185
+ #
186
+ # @return [Mash] A new mash with the hash values merged in.
187
+ def merge(hash)
188
+ dup.update(hash)
189
+ end
190
+
191
+ # @param key<Object>
192
+ # The key to delete from the mash.\
193
+ def delete(key)
194
+ super(convert_key(key))
195
+ end
196
+
197
+ # @param *rejected<Array[(String, Symbol)] The mash keys to exclude.
198
+ #
199
+ # @return [Mash] A new mash without the selected keys.
200
+ #
201
+ # @example
202
+ # { :one => 1, :two => 2, :three => 3 }.except(:one)
203
+ # #=> { "two" => 2, "three" => 3 }
204
+ def except(*keys)
205
+ super(*keys.map { |k| convert_key(k) })
206
+ end
207
+
208
+ # Used to provide the same interface as Hash.
209
+ #
210
+ # @return [Mash] This mash unchanged.
211
+ def stringify_keys!; self end
212
+
213
+ # @return [Hash] The mash as a Hash with symbolized keys.
214
+ def symbolize_keys
215
+ h = Hash.new(default)
216
+ each { |key, val| h[key.to_sym] = val }
217
+ h
218
+ end
219
+
220
+ # @return [Hash] The mash as a Hash with string keys.
221
+ def to_hash
222
+ Hash.new(default).merge(self)
223
+ end
224
+
225
+ # @return [Mash] Convert a Hash into a Mash
226
+ # The input Hash's default value is maintained
227
+ def self.from_hash(hash)
228
+ mash = Mash.new(hash)
229
+ mash.default = hash.default
230
+ mash
231
+ end
232
+
233
+ protected
234
+
235
+ # @param key<Object> The key to convert.
236
+ #
237
+ # @param [Object]
238
+ # The converted key. If the key was a symbol, it will be converted to a
239
+ # string.
240
+ #
241
+ # @api private
242
+ def convert_key(key)
243
+ key.is_a?(Symbol) ? key.to_s : key
244
+ end
245
+
246
+ # @param value<Object> The value to convert.
247
+ #
248
+ # @return [Object]
249
+ # The converted value. A Hash or an Array of hashes, will be converted to
250
+ # their Mash equivalents.
251
+ #
252
+ # @api private
253
+ def convert_value(value)
254
+ if value.class == Hash
255
+ Mash.from_hash(value)
256
+ elsif value.is_a?(Array)
257
+ value.collect { |e| convert_value(e) }
258
+ else
259
+ value
260
+ end
261
+ end
262
+ end
263
+ end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright:: Copyright (c) Chef Software Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require "concurrent/executors"
20
+ require "concurrent/future"
21
+ require "singleton" unless defined?(Singleton)
22
+
23
+ module ChefUtils
24
+ #
25
+ # This module contains ruby refinements that adds several methods to the Enumerable
26
+ # class which are useful for parallel processing.
27
+ #
28
+ module ParallelMap
29
+ refine Enumerable do
30
+
31
+ # Enumerates through the collection in parallel using the thread pool provided
32
+ # or the default thread pool. By using the default thread pool this supports
33
+ # recursively calling the method without deadlocking while using a globally
34
+ # fixed number of workers. This method supports lazy collections. It returns
35
+ # synchronously, waiting until all the work is done. Failures are only reported
36
+ # after the collection has executed and only the first exception is raised.
37
+ #
38
+ # (0..).lazy.parallel_map { |i| i*i }.first(5)
39
+ #
40
+ # @return [Array] output results
41
+ #
42
+ def parallel_map(pool: nil)
43
+ return self unless block_given?
44
+
45
+ pool ||= ChefUtils::DefaultThreadPool.instance.pool
46
+
47
+ futures = map do |item|
48
+ Concurrent::Future.execute(executor: pool) do
49
+ yield item
50
+ end
51
+ end
52
+
53
+ futures.map(&:value!)
54
+ end
55
+
56
+ # This has the same behavior as parallel_map but returns the enumerator instead of
57
+ # the return values.
58
+ #
59
+ # @return [Enumerable] the enumerable for method chaining
60
+ #
61
+ def parallel_each(pool: nil, &block)
62
+ return self unless block_given?
63
+
64
+ parallel_map(pool: pool, &block)
65
+
66
+ self
67
+ end
68
+
69
+ # The flat_each method is tightly coupled to the usage of parallel_map within the
70
+ # ChefFS implementation. It is not itself a parallel method, but it is used to
71
+ # iterate through the 2nd level of nested structure, which is tied to the nested
72
+ # structures that ChefFS returns.
73
+ #
74
+ # This is different from Enumerable#flat_map because that behaves like map.flatten(1) while
75
+ # this behaves more like flatten(1).each. We need this on an Enumerable, so we have no
76
+ # Enumerable#flatten method to call.
77
+ #
78
+ # [ [ 1, 2 ], [ 3, 4 ] ].flat_each(&block) calls block four times with 1, 2, 3, 4
79
+ #
80
+ # [ [ 1, 2 ], [ 3, 4 ] ].flat_map(&block) calls block twice with [1, 2] and [3,4]
81
+ #
82
+ def flat_each(&block)
83
+ map do |value|
84
+ if value.is_a?(Enumerable)
85
+ value.each(&block)
86
+ else
87
+ yield value
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ # The DefaultThreadPool has a fixed thread size and has no
95
+ # queue of work and the behavior on failure to find a thread is for the
96
+ # caller to run the work. This contract means that the thread pool can
97
+ # be called recursively without deadlocking and while keeping the fixed
98
+ # number of threads (and not exponentially growing the thread pool with
99
+ # the depth of recursion).
100
+ #
101
+ class DefaultThreadPool
102
+ include Singleton
103
+
104
+ DEFAULT_THREAD_SIZE = 10
105
+
106
+ # Size of the thread pool, must be set before getting the thread pool or
107
+ # calling parallel_map/parallel_each. Does not (but could be modified to)
108
+ # support dynamic resizing. To get fully synchronous behavior set this equal to
109
+ # zero rather than one since the caller will get work if the threads are
110
+ # busy.
111
+ #
112
+ # @return [Integer] number of threads
113
+ attr_accessor :threads
114
+
115
+ # Memoizing accessor for the thread pool
116
+ #
117
+ # @return [Concurrent::ThreadPoolExecutor] the thread pool
118
+ def pool
119
+ @pool ||= Concurrent::ThreadPoolExecutor.new(
120
+ min_threads: threads || DEFAULT_THREAD_SIZE,
121
+ max_threads: threads || DEFAULT_THREAD_SIZE,
122
+ max_queue: 0,
123
+ # "synchronous" redefines the 0 in max_queue to mean 'no queue' instead of 'infinite queue'
124
+ # it does not mean synchronous execution (no threads) but synchronous offload to the threads.
125
+ synchronous: true,
126
+ # this prevents deadlocks on recursive parallel usage
127
+ fallback_policy: :caller_runs
128
+ )
129
+ end
130
+ end
131
+ end