mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
Add Object#deep_dup to extend/
This commit is contained in:
parent
f6c7eb7124
commit
8efe73b32c
11
.gitignore
vendored
11
.gitignore
vendored
@ -57,14 +57,7 @@
|
|||||||
!**/vendor/bundle/ruby/*/gems/rubocop-rails-*/config
|
!**/vendor/bundle/ruby/*/gems/rubocop-rails-*/config
|
||||||
!**/vendor/bundle/ruby/*/gems/rubocop-rspec-*/config
|
!**/vendor/bundle/ruby/*/gems/rubocop-rspec-*/config
|
||||||
!**/vendor/bundle/ruby/*/gems/rubocop-sorbet-*/config
|
!**/vendor/bundle/ruby/*/gems/rubocop-sorbet-*/config
|
||||||
|
!**/vendor/bundle/ruby/*/gems/sorbet-runtime-*/
|
||||||
# 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
|
|
||||||
|
|
||||||
# Ignore partially included gems where we don't need all files
|
# Ignore partially included gems where we don't need all files
|
||||||
**/vendor/gems/mechanize-*/.*
|
**/vendor/gems/mechanize-*/.*
|
||||||
@ -82,6 +75,7 @@
|
|||||||
**/vendor/gems/mechanize-*/test/
|
**/vendor/gems/mechanize-*/test/
|
||||||
|
|
||||||
# Ignore dependencies we don't wish to vendor
|
# Ignore dependencies we don't wish to vendor
|
||||||
|
**/vendor/bundle/ruby/*/gems/activesupport-*/
|
||||||
**/vendor/bundle/ruby/*/gems/ast-*/
|
**/vendor/bundle/ruby/*/gems/ast-*/
|
||||||
**/vendor/bundle/ruby/*/gems/bootsnap-*/
|
**/vendor/bundle/ruby/*/gems/bootsnap-*/
|
||||||
**/vendor/bundle/ruby/*/gems/bundler-*/
|
**/vendor/bundle/ruby/*/gems/bundler-*/
|
||||||
@ -144,7 +138,6 @@
|
|||||||
**/vendor/bundle/ruby/*/gems/simplecov_json_formatter-*/
|
**/vendor/bundle/ruby/*/gems/simplecov_json_formatter-*/
|
||||||
**/vendor/bundle/ruby/*/gems/simpleidn-*/
|
**/vendor/bundle/ruby/*/gems/simpleidn-*/
|
||||||
**/vendor/bundle/ruby/*/gems/sorbet-*/
|
**/vendor/bundle/ruby/*/gems/sorbet-*/
|
||||||
!**/vendor/bundle/ruby/*/gems/sorbet-runtime-*/
|
|
||||||
**/vendor/bundle/ruby/*/gems/spoom-*/
|
**/vendor/bundle/ruby/*/gems/spoom-*/
|
||||||
**/vendor/bundle/ruby/*/gems/stackprof-*/
|
**/vendor/bundle/ruby/*/gems/stackprof-*/
|
||||||
**/vendor/bundle/ruby/*/gems/strscan-*/
|
**/vendor/bundle/ruby/*/gems/strscan-*/
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "attrable"
|
require "attrable"
|
||||||
require "active_support/core_ext/object/deep_dup"
|
require "extend/object/deep_dup"
|
||||||
|
|
||||||
module Cask
|
module Cask
|
||||||
module Artifact
|
module Artifact
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
|
# typed: strict
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "active_support/core_ext/object/duplicable"
|
require "extend/object/duplicable"
|
||||||
|
|
||||||
class Object
|
class Object
|
||||||
# Returns a deep copy of object if it's duplicable. If it's
|
# 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
|
# object.instance_variable_defined?(:@a) # => false
|
||||||
# dup.instance_variable_defined?(:@a) # => true
|
# dup.instance_variable_defined?(:@a) # => true
|
||||||
def deep_dup
|
sig { returns(T.self_type) }
|
||||||
duplicable? ? dup : self
|
def deep_dup = duplicable? ? dup : self
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class Array
|
class Array
|
||||||
@ -26,9 +26,8 @@ class Array
|
|||||||
#
|
#
|
||||||
# array[1][2] # => nil
|
# array[1][2] # => nil
|
||||||
# dup[1][2] # => 4
|
# dup[1][2] # => 4
|
||||||
def deep_dup
|
sig { returns(T.self_type) }
|
||||||
map(&:deep_dup)
|
def deep_dup = T.unsafe(self).map(&:deep_dup)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class Hash
|
class Hash
|
||||||
@ -40,16 +39,35 @@ class Hash
|
|||||||
#
|
#
|
||||||
# hash[:a][:c] # => nil
|
# hash[:a][:c] # => nil
|
||||||
# dup[:a][:c] # => "c"
|
# dup[:a][:c] # => "c"
|
||||||
|
sig { returns(T.self_type) }
|
||||||
def deep_dup
|
def deep_dup
|
||||||
hash = dup
|
hash = dup
|
||||||
each_pair do |key, value|
|
each_pair do |key, value|
|
||||||
if (::String === key && key.frozen?) || ::Symbol === key
|
case key
|
||||||
hash[key] = value.deep_dup
|
when ::String, ::Symbol
|
||||||
|
hash[key] = T.unsafe(value).deep_dup
|
||||||
else
|
else
|
||||||
hash.delete(key)
|
hash.delete(key)
|
||||||
hash[key.deep_dup] = value.deep_dup
|
hash[T.unsafe(key).deep_dup] = T.unsafe(value).deep_dup
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
hash
|
hash
|
||||||
end
|
end
|
||||||
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
|
@ -1,3 +1,4 @@
|
|||||||
|
# typed: strict
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
#--
|
#--
|
||||||
@ -23,6 +24,7 @@ class Object
|
|||||||
#
|
#
|
||||||
# False for method objects;
|
# False for method objects;
|
||||||
# true otherwise.
|
# true otherwise.
|
||||||
|
sig { returns(T::Boolean) }
|
||||||
def duplicable?
|
def duplicable?
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
@ -33,6 +35,7 @@ class Method
|
|||||||
#
|
#
|
||||||
# method(:puts).duplicable? # => false
|
# method(:puts).duplicable? # => false
|
||||||
# method(:puts).dup # => TypeError: allocator undefined for Method
|
# method(:puts).dup # => TypeError: allocator undefined for Method
|
||||||
|
sig { returns(FalseClass) }
|
||||||
def duplicable?
|
def duplicable?
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
@ -43,6 +46,19 @@ class UnboundMethod
|
|||||||
#
|
#
|
||||||
# method(:puts).unbind.duplicable? # => false
|
# method(:puts).unbind.duplicable? # => false
|
||||||
# method(:puts).unbind.dup # => TypeError: allocator undefined for UnboundMethod
|
# 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?
|
def duplicable?
|
||||||
false
|
false
|
||||||
end
|
end
|
@ -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.
|
|
Loading…
x
Reference in New Issue
Block a user