Skip to content

Commit

Permalink
Add Hash#deep_merge to extend/
Browse files Browse the repository at this point in the history
  • Loading branch information
dduugg committed Jan 12, 2024
1 parent 32f243a commit f29324e
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 8 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@
!**/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/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/keys.rb
!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/object/deep_dup.rb
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/dev-cmd/bottle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require "erb"
require "utils/gzip"
require "api"
require "extend/hash/deep_merge"

BOTTLE_ERB = <<-EOS.freeze
bottle do
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/dev-cmd/pr-upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "formula"
require "github_packages"
require "github_releases"
require "extend/hash/deep_merge"

module Homebrew
module_function
Expand Down
5 changes: 5 additions & 0 deletions Library/Homebrew/extend/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def third = self[2]
# %w( a b c d e ).fourth # => "d"
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
# joined by the connector word.
#
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/extend/array.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ class Array

sig { returns(T.nilable(Elem)) }
def fourth; end

sig { returns(T.nilable(Elem)) }
def fifth; end
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# typed: strict
# frozen_string_literal: true

class Hash
Expand All @@ -22,10 +23,10 @@ def deep_merge(other_hash, &block)
# Same as +deep_merge+, but modifies +self+.
def deep_merge!(other_hash, &block)
merge!(other_hash) do |key, this_val, other_val|
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
this_val.deep_merge(other_val, &block)
elsif block_given?
block.call(key, this_val, other_val)
if T.unsafe(this_val).is_a?(Hash) && other_val.is_a?(Hash)
T.unsafe(this_val).deep_merge(other_val, &block)
elsif block
yield(key, this_val, other_val)
else
other_val
end
Expand Down
20 changes: 20 additions & 0 deletions Library/Homebrew/extend/hash/deep_merge.rbi
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion Library/Homebrew/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
require "forwardable"
require "set"

require "active_support/core_ext/hash/deep_merge"
require "active_support/core_ext/hash/keys"

HOMEBREW_API_DEFAULT_DOMAIN = ENV.fetch("HOMEBREW_API_DEFAULT_DOMAIN").freeze
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/test/rubocop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
end

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(stdout).to include("no offenses detected")
expect(status).to be_a_success
Expand Down

0 comments on commit f29324e

Please sign in to comment.