From 6fdba65f4950829e2abd8b1730c32d8dcc1a736d Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Fri, 12 Jan 2024 09:53:53 -0800 Subject: [PATCH] Add Object#deep_dup to extend/ --- .gitignore | 11 +----- .../cask/artifact/abstract_artifact.rb | 2 +- .../core_ext => extend}/object/deep_dup.rb | 38 ++++++++++++++----- .../core_ext => extend}/object/duplicable.rb | 24 ++++++++++-- 4 files changed, 51 insertions(+), 24 deletions(-) rename Library/Homebrew/{vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7.6/lib/active_support/core_ext => extend}/object/deep_dup.rb (56%) rename Library/Homebrew/{vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7.6/lib/active_support/core_ext => extend}/object/duplicable.rb (63%) diff --git a/.gitignore b/.gitignore index a88cbb4ca780be..2172e87ce92e9d 100644 --- a/.gitignore +++ b/.gitignore @@ -57,14 +57,7 @@ !**/vendor/bundle/ruby/*/gems/rubocop-rails-*/config !**/vendor/bundle/ruby/*/gems/rubocop-rspec-*/config !**/vendor/bundle/ruby/*/gems/rubocop-sorbet-*/config - -# Ignore activesupport, except the ones we need. -**/vendor/bundle/ruby/*/gems/activesupport-*/lib/**/* -!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/ -!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/ -!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/*/ -!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/object/deep_dup.rb -!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/object/duplicable.rb +!**/vendor/bundle/ruby/*/gems/sorbet-runtime-*/ # Ignore partially included gems where we don't need all files **/vendor/gems/mechanize-*/.* @@ -82,6 +75,7 @@ **/vendor/gems/mechanize-*/test/ # Ignore dependencies we don't wish to vendor +**/vendor/bundle/ruby/*/gems/activesupport-*/ **/vendor/bundle/ruby/*/gems/ast-*/ **/vendor/bundle/ruby/*/gems/bootsnap-*/ **/vendor/bundle/ruby/*/gems/bundler-*/ @@ -144,7 +138,6 @@ **/vendor/bundle/ruby/*/gems/simplecov_json_formatter-*/ **/vendor/bundle/ruby/*/gems/simpleidn-*/ **/vendor/bundle/ruby/*/gems/sorbet-*/ -!**/vendor/bundle/ruby/*/gems/sorbet-runtime-*/ **/vendor/bundle/ruby/*/gems/spoom-*/ **/vendor/bundle/ruby/*/gems/stackprof-*/ **/vendor/bundle/ruby/*/gems/strscan-*/ diff --git a/Library/Homebrew/cask/artifact/abstract_artifact.rb b/Library/Homebrew/cask/artifact/abstract_artifact.rb index 7e86cf4ac58a7f..dcfddaca462234 100644 --- a/Library/Homebrew/cask/artifact/abstract_artifact.rb +++ b/Library/Homebrew/cask/artifact/abstract_artifact.rb @@ -2,7 +2,7 @@ # frozen_string_literal: true require "attrable" -require "active_support/core_ext/object/deep_dup" +require "extend/object/deep_dup" module Cask module Artifact diff --git a/Library/Homebrew/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7.6/lib/active_support/core_ext/object/deep_dup.rb b/Library/Homebrew/extend/object/deep_dup.rb similarity index 56% rename from Library/Homebrew/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7.6/lib/active_support/core_ext/object/deep_dup.rb rename to Library/Homebrew/extend/object/deep_dup.rb index 5c903917f5f066..b64eccc5f70263 100644 --- a/Library/Homebrew/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7.6/lib/active_support/core_ext/object/deep_dup.rb +++ b/Library/Homebrew/extend/object/deep_dup.rb @@ -1,6 +1,7 @@ +# typed: strict # frozen_string_literal: true -require "active_support/core_ext/object/duplicable" +require "extend/object/duplicable" class Object # Returns a deep copy of object if it's duplicable. If it's @@ -12,9 +13,8 @@ class Object # # object.instance_variable_defined?(:@a) # => false # dup.instance_variable_defined?(:@a) # => true - def deep_dup - duplicable? ? dup : self - end + sig { returns(T.self_type) } + def deep_dup = duplicable? ? dup : self end class Array @@ -26,9 +26,8 @@ class Array # # array[1][2] # => nil # dup[1][2] # => 4 - def deep_dup - map(&:deep_dup) - end + sig { returns(T.self_type) } + def deep_dup = T.unsafe(self).map(&:deep_dup) end class Hash @@ -40,16 +39,35 @@ class Hash # # hash[:a][:c] # => nil # dup[:a][:c] # => "c" + sig { returns(T.self_type) } def deep_dup hash = dup each_pair do |key, value| - if (::String === key && key.frozen?) || ::Symbol === key - hash[key] = value.deep_dup + case key + when ::String, ::Symbol + hash[key] = T.unsafe(value).deep_dup else hash.delete(key) - hash[key.deep_dup] = value.deep_dup + hash[T.unsafe(key).deep_dup] = T.unsafe(value).deep_dup end end hash end end + +class Module + # Returns a copy of module or class if it's anonymous. If it's + # named, returns +self+. + # + # Object.deep_dup == Object # => true + # klass = Class.new + # klass.deep_dup == klass # => false + sig { returns(T.self_type) } + def deep_dup + if name.nil? + super + else + self + end + end +end diff --git a/Library/Homebrew/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7.6/lib/active_support/core_ext/object/duplicable.rb b/Library/Homebrew/extend/object/duplicable.rb similarity index 63% rename from Library/Homebrew/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7.6/lib/active_support/core_ext/object/duplicable.rb rename to Library/Homebrew/extend/object/duplicable.rb index 3ebcdca02b73cd..0ab1d52e837908 100644 --- a/Library/Homebrew/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7.6/lib/active_support/core_ext/object/duplicable.rb +++ b/Library/Homebrew/extend/object/duplicable.rb @@ -1,3 +1,4 @@ +# typed: strict # frozen_string_literal: true #-- @@ -23,6 +24,7 @@ class Object # # False for method objects; # true otherwise. + sig { returns(T::Boolean) } def duplicable? true end @@ -31,8 +33,9 @@ def duplicable? class Method # Methods are not duplicable: # - # method(:puts).duplicable? # => false - # method(:puts).dup # => TypeError: allocator undefined for Method + # method(:puts).duplicable? # => false + # method(:puts).dup # => TypeError: allocator undefined for Method + sig { returns(FalseClass) } def duplicable? false end @@ -41,8 +44,21 @@ def duplicable? class UnboundMethod # Unbound methods are not duplicable: # - # method(:puts).unbind.duplicable? # => false - # method(:puts).unbind.dup # => TypeError: allocator undefined for UnboundMethod + # method(:puts).unbind.duplicable? # => false + # method(:puts).unbind.dup # => TypeError: allocator undefined for UnboundMethod + sig { returns(FalseClass) } + def duplicable? + false + end +end + +require "singleton" + +module Singleton + # Singleton instances are not duplicable: + # + # Class.new.include(Singleton).instance.dup # TypeError (can't dup instance of singleton + sig { returns(FalseClass) } def duplicable? false end