| 
									
										
										
										
											2024-01-11 20:03:06 -08:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2023-11-29 15:18:14 +00:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Hash | 
					
						
							|  |  |  |   # Returns a new hash with all values converted by the block operation. | 
					
						
							|  |  |  |   # This includes the values from the root hash and from all | 
					
						
							|  |  |  |   # nested hashes and arrays. | 
					
						
							|  |  |  |   # | 
					
						
							| 
									
										
										
										
											2024-03-17 20:53:58 -07:00
										 |  |  |   # @example | 
					
						
							| 
									
										
										
										
											2023-11-29 15:18:14 +00:00
										 |  |  |   #  hash = { person: { name: 'Rob', age: '28' } } | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   #  hash.deep_transform_values{ |value| value.to_s.upcase } | 
					
						
							|  |  |  |   #  # => {person: {name: "ROB", age: "28"}} | 
					
						
							| 
									
										
										
										
											2024-01-11 20:04:14 -08:00
										 |  |  |   def deep_transform_values(&block) = _deep_transform_values_in_object(self, &block) | 
					
						
							| 
									
										
										
										
											2023-11-29 15:18:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # Destructively converts all values by using the block operation. | 
					
						
							|  |  |  |   # This includes the values from the root hash and from all | 
					
						
							|  |  |  |   # nested hashes and arrays. | 
					
						
							| 
									
										
										
										
											2024-01-11 20:04:14 -08:00
										 |  |  |   def deep_transform_values!(&block) = _deep_transform_values_in_object!(self, &block) | 
					
						
							| 
									
										
										
										
											2023-11-29 15:18:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   private | 
					
						
							| 
									
										
										
										
											2024-01-11 20:04:14 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # Support methods for deep transforming nested hashes and arrays. | 
					
						
							| 
									
										
										
										
											2024-01-12 09:38:49 -08:00
										 |  |  |   sig { params(object: T.anything, block: T.proc.params(v: T.untyped).returns(T.untyped)).returns(T.untyped) } | 
					
						
							| 
									
										
										
										
											2024-01-11 20:04:14 -08:00
										 |  |  |   def _deep_transform_values_in_object(object, &block) | 
					
						
							|  |  |  |     case object | 
					
						
							|  |  |  |     when Hash | 
					
						
							|  |  |  |       object.transform_values { |value| _deep_transform_values_in_object(value, &block) } | 
					
						
							|  |  |  |     when Array | 
					
						
							|  |  |  |       object.map { |e| _deep_transform_values_in_object(e, &block) } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       yield(object) | 
					
						
							| 
									
										
										
										
											2023-11-29 15:18:14 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2024-01-11 20:04:14 -08:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2023-11-29 15:18:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-12 09:38:49 -08:00
										 |  |  |   sig { params(object: T.anything, block: T.proc.params(v: T.untyped).returns(T.untyped)).returns(T.untyped) } | 
					
						
							| 
									
										
										
										
											2024-01-11 20:04:14 -08:00
										 |  |  |   def _deep_transform_values_in_object!(object, &block) | 
					
						
							|  |  |  |     case object | 
					
						
							|  |  |  |     when Hash | 
					
						
							|  |  |  |       object.transform_values! { |value| _deep_transform_values_in_object!(value, &block) } | 
					
						
							|  |  |  |     when Array | 
					
						
							|  |  |  |       object.map! { |e| _deep_transform_values_in_object!(e, &block) } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       yield(object) | 
					
						
							| 
									
										
										
										
											2023-11-29 15:18:14 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2024-01-11 20:04:14 -08:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2023-11-29 15:18:14 +00:00
										 |  |  | end |