| 
									
										
										
										
											2024-08-12 10:30:59 +01:00
										 |  |  | # typed: true # rubocop:todo Sorbet/StrictSigil | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2021-02-12 18:33:37 +05:30
										 |  |  |     if (current_context = Thread.current[:context]) | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |       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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-10 03:39:42 +02:00
										 |  |  |     sig { returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |     def debug? | 
					
						
							| 
									
										
										
										
											2020-11-23 02:05:50 +01:00
										 |  |  |       @debug == true | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-10 03:39:42 +02:00
										 |  |  |     sig { returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |     def quiet? | 
					
						
							| 
									
										
										
										
											2020-11-23 02:05:50 +01:00
										 |  |  |       @quiet == true | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-10 03:39:42 +02:00
										 |  |  |     sig { returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |     def verbose? | 
					
						
							| 
									
										
										
										
											2020-11-23 02:05:50 +01:00
										 |  |  |       @verbose == true | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-10 03:39:42 +02:00
										 |  |  |   sig { returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |   def debug? | 
					
						
							|  |  |  |     Context.current.debug? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-10 03:39:42 +02:00
										 |  |  |   sig { returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |   def quiet? | 
					
						
							|  |  |  |     Context.current.quiet? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-10 03:39:42 +02:00
										 |  |  |   sig { returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |   def verbose? | 
					
						
							|  |  |  |     Context.current.verbose? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def with_context(**options) | 
					
						
							| 
									
										
										
										
											2024-07-08 13:18:18 -04:00
										 |  |  |     old_context = Context.current | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-10 03:39:42 +02:00
										 |  |  |     begin | 
					
						
							|  |  |  |       yield | 
					
						
							|  |  |  |     ensure | 
					
						
							|  |  |  |       Thread.current[:context] = old_context | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | end |