| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "monitor" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-14 03:26:20 +02:00
										 |  |  | # Module for querying the current execution context. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # @api private | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  | module Context | 
					
						
							|  |  |  |   extend MonitorMixin | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def self.current=(context) | 
					
						
							|  |  |  |     synchronize do | 
					
						
							|  |  |  |       @current = context | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def self.current | 
					
						
							|  |  |  |     if current_context = Thread.current[:context] | 
					
						
							|  |  |  |       return current_context | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     synchronize do | 
					
						
							|  |  |  |       @current ||= ContextStruct.new | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-14 03:26:20 +02:00
										 |  |  |   # Struct describing the current execution context. | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |   class ContextStruct | 
					
						
							|  |  |  |     def initialize(debug: nil, quiet: nil, verbose: nil) | 
					
						
							|  |  |  |       @debug = debug | 
					
						
							|  |  |  |       @quiet = quiet | 
					
						
							|  |  |  |       @verbose = verbose | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def debug? | 
					
						
							| 
									
										
										
										
											2020-11-23 02:05:50 +01:00
										 |  |  |       @debug == true | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def quiet? | 
					
						
							| 
									
										
										
										
											2020-11-23 02:05:50 +01:00
										 |  |  |       @quiet == true | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def verbose? | 
					
						
							| 
									
										
										
										
											2020-11-23 02:05:50 +01:00
										 |  |  |       @verbose == true | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def debug? | 
					
						
							|  |  |  |     Context.current.debug? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def quiet? | 
					
						
							|  |  |  |     Context.current.quiet? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def verbose? | 
					
						
							|  |  |  |     Context.current.verbose? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def with_context(**options) | 
					
						
							|  |  |  |     old_context = Thread.current[:context] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     new_context = ContextStruct.new( | 
					
						
							|  |  |  |       debug:   options.fetch(:debug, old_context&.debug?), | 
					
						
							|  |  |  |       quiet:   options.fetch(:quiet, old_context&.quiet?), | 
					
						
							|  |  |  |       verbose: options.fetch(:verbose, old_context&.verbose?), | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Thread.current[:context] = new_context | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     yield | 
					
						
							|  |  |  |   ensure | 
					
						
							|  |  |  |     Thread.current[:context] = old_context | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |