Add Hash#deep_merge to extend/

This commit is contained in:
Douglas Eichelberger 2024-01-11 19:22:16 -08:00
parent 8e9d294df2
commit 0cdd4eee3b
9 changed files with 36 additions and 8 deletions

2
.gitignore vendored
View File

@ -63,8 +63,6 @@
!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/ !**/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/*/ !**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/*/
!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/file/atomic.rb
!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/hash/deep_merge.rb
!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/hash/deep_transform_values.rb !**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/hash/deep_transform_values.rb
!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/hash/keys.rb !**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/hash/keys.rb
!**/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/deep_dup.rb

View File

@ -11,6 +11,7 @@ require "utils/inreplace"
require "erb" require "erb"
require "utils/gzip" require "utils/gzip"
require "api" require "api"
require "extend/hash/deep_merge"
BOTTLE_ERB = <<-EOS.freeze BOTTLE_ERB = <<-EOS.freeze
bottle do bottle do

View File

@ -5,6 +5,7 @@ require "cli/parser"
require "formula" require "formula"
require "github_packages" require "github_packages"
require "github_releases" require "github_releases"
require "extend/hash/deep_merge"
module Homebrew module Homebrew
module_function module_function

View File

@ -17,6 +17,11 @@ class Array
# %w( a b c d e ).fourth # => "d" # %w( a b c d e ).fourth # => "d"
def fourth = self[3] def fourth = self[3]
# Equal to <tt>self[4]</tt>.
#
# %w( a b c d e ).fifth # => "e"
def fifth = self[4]
# Converts the array to a comma-separated sentence where the last element is # Converts the array to a comma-separated sentence where the last element is
# joined by the connector word. # joined by the connector word.
# #

View File

@ -9,4 +9,7 @@ class Array
sig { returns(T.nilable(Elem)) } sig { returns(T.nilable(Elem)) }
def fourth; end def fourth; end
sig { returns(T.nilable(Elem)) }
def fifth; end
end end

View File

@ -1,3 +1,4 @@
# typed: strict
# frozen_string_literal: true # frozen_string_literal: true
class Hash class Hash
@ -22,10 +23,10 @@ class Hash
# Same as +deep_merge+, but modifies +self+. # Same as +deep_merge+, but modifies +self+.
def deep_merge!(other_hash, &block) def deep_merge!(other_hash, &block)
merge!(other_hash) do |key, this_val, other_val| merge!(other_hash) do |key, this_val, other_val|
if this_val.is_a?(Hash) && other_val.is_a?(Hash) if T.unsafe(this_val).is_a?(Hash) && other_val.is_a?(Hash)
this_val.deep_merge(other_val, &block) T.unsafe(this_val).deep_merge(other_val, &block)
elsif block_given? elsif block
block.call(key, this_val, other_val) yield(key, this_val, other_val)
else else
other_val other_val
end end

View File

@ -0,0 +1,20 @@
# typed: strict
# frozen_string_literal: true
class Hash
sig do
type_parameters(:k2).params(
other_hash: T::Hash[T.type_parameter(:k2), T.untyped],
block: T.nilable(T.proc.params(k: T.untyped, v1: T.untyped, v2: T.untyped).returns(T.untyped))
).returns(T::Hash[T.any(Hash::K, T.type_parameter(:k2)), T.untyped])
end
def deep_merge(other_hash, &block); end
sig do
type_parameters(:k2).params(
other_hash: T::Hash[T.type_parameter(:k2), T.untyped],
block: T.nilable(T.proc.params(k: T.untyped, v1: T.untyped, v2: T.untyped).returns(T.untyped))
).returns(T::Hash[T.any(Hash::K, T.type_parameter(:k2)), T.untyped])
end
def deep_merge!(other_hash, &block); end
end

View File

@ -10,7 +10,6 @@ require "json/add/exception"
require "forwardable" require "forwardable"
require "set" require "set"
require "active_support/core_ext/hash/deep_merge"
require "active_support/core_ext/hash/keys" require "active_support/core_ext/hash/keys"
HOMEBREW_API_DEFAULT_DOMAIN = ENV.fetch("HOMEBREW_API_DEFAULT_DOMAIN").freeze HOMEBREW_API_DEFAULT_DOMAIN = ENV.fetch("HOMEBREW_API_DEFAULT_DOMAIN").freeze

View File

@ -17,7 +17,7 @@ describe "RuboCop" do
end end
it "loads all Formula cops without errors" do it "loads all Formula cops without errors" do
stdout, stderr, status = Open3.capture3(RUBY_PATH, "-W0", "-S", "rubocop", TEST_FIXTURE_DIR/"testball.rb") stdout, stderr, status = Open3.capture3(RUBY_PATH, "-W0", "-S", "rubocop", "-d", TEST_FIXTURE_DIR/"testball.rb")
expect(stderr).to be_empty expect(stderr).to be_empty
expect(stdout).to include("no offenses detected") expect(stdout).to include("no offenses detected")
expect(status).to be_a_success expect(status).to be_a_success