Add Object#deep_dup to extend/

This commit is contained in:
Douglas Eichelberger 2024-01-12 09:53:53 -08:00
parent f6c7eb7124
commit 8efe73b32c
5 changed files with 51 additions and 44 deletions

11
.gitignore vendored
View File

@ -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-*/

View File

@ -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

View File

@ -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

View File

@ -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 @@ end
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 @@ end
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

View File

@ -1,20 +0,0 @@
Copyright (c) 2005-2022 David Heinemeier Hansson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.