Remove use of ActiveSupport try

This commit is contained in:
Douglas Eichelberger 2023-10-08 09:36:08 -07:00
parent e8290c2ed4
commit 00ba09d73d
9 changed files with 17 additions and 170 deletions

1
.gitignore vendored
View File

@ -73,7 +73,6 @@
!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/object/blank.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/duplicable.rb
!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/object/try.rb
!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/string/exclude.rb
!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/string/filters.rb
!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/string/indent.rb

View File

@ -20,7 +20,7 @@ module Cask
raise CaskInvalidError.new(cask.token, "No source provided for #{english_name}.") if source.blank?
unless options.try(:key?, :target)
unless options&.key?(:target)
raise CaskInvalidError.new(cask.token, "#{english_name} '#{source}' requires a target.")
end

View File

@ -300,12 +300,12 @@ on_request: true)
def missing_cask_and_formula_dependencies
cask_and_formula_dependencies.reject do |cask_or_formula|
installed = if cask_or_formula.respond_to?(:any_version_installed?)
cask_or_formula.any_version_installed?
else
cask_or_formula.try(:installed?)
case cask_or_formula
when Formula
cask_or_formula.any_version_installed? && cask_or_formula.optlinked?
when Cask
cask_or_formula.installed?
end
installed && (cask_or_formula.respond_to?(:optlinked?) ? cask_or_formula.optlinked? : true)
end
end

View File

@ -279,7 +279,7 @@ module Homebrew
def retrieve_pull_requests(formula_or_cask, name, state:, version: nil)
tap_remote_repo = formula_or_cask.tap&.remote_repo || formula_or_cask.tap&.full_name
pull_requests = GitHub.fetch_pull_requests(name, tap_remote_repo, state: state, version: version)
if pull_requests.try(:any?)
if pull_requests&.any?
pull_requests = pull_requests.map { |pr| "#{pr["title"]} (#{Formatter.url(pr["html_url"])})" }.join(", ")
end

View File

@ -288,7 +288,7 @@ module SharedEnvExtension
gcc_version_name = "gcc@#{version}"
gcc = Formulary.factory("gcc")
if gcc.try(:version_suffix) == version
if gcc.respond_to?(:version_suffix) && T.unsafe(gcc).version_suffix == version
gcc
else
Formulary.factory(gcc_version_name)

View File

@ -2351,12 +2351,17 @@ class Formula
hsh["requirements"] = merge_spec_dependables(requirements).map do |data|
req = data[:dependable]
req_name = req.name.dup
req_name.prepend("maximum_") if req.try(:comparator) == "<="
req_name.prepend("maximum_") if req.respond_to?(:comparator) && req.comparator == "<="
req_version = if req.respond_to?(:version)
req.version
elsif req.respond_to?(:arch)
req.arch
end
{
"name" => req_name,
"cask" => req.cask,
"download" => req.download,
"version" => req.try(:version) || req.try(:arch),
"version" => req_version,
"contexts" => req.tags,
"specs" => data[:specs],
}

View File

@ -19,7 +19,6 @@ require "active_support/core_ext/hash/deep_merge"
require "active_support/core_ext/hash/except"
require "active_support/core_ext/hash/keys"
require "active_support/core_ext/object/blank"
require "active_support/core_ext/object/try"
require "active_support/core_ext/string/exclude"
require "active_support/core_ext/string/filters"
require "active_support/core_ext/string/indent"

View File

@ -1,6 +1,8 @@
# typed: true
# frozen_string_literal: true
require "delegate"
# An object which lazily evaluates its inner block only once a method is called on it.
#
# @api private

View File

@ -1,158 +0,0 @@
# frozen_string_literal: true
require "delegate"
module ActiveSupport
module Tryable #:nodoc:
def try(method_name = nil, *args, &b)
if method_name.nil? && block_given?
if b.arity == 0
instance_eval(&b)
else
yield self
end
elsif respond_to?(method_name)
public_send(method_name, *args, &b)
end
end
ruby2_keywords(:try) if respond_to?(:ruby2_keywords, true)
def try!(method_name = nil, *args, &b)
if method_name.nil? && block_given?
if b.arity == 0
instance_eval(&b)
else
yield self
end
else
public_send(method_name, *args, &b)
end
end
ruby2_keywords(:try!) if respond_to?(:ruby2_keywords, true)
end
end
class Object
include ActiveSupport::Tryable
##
# :method: try
#
# :call-seq:
# try(*a, &b)
#
# Invokes the public method whose name goes as first argument just like
# +public_send+ does, except that if the receiver does not respond to it the
# call returns +nil+ rather than raising an exception.
#
# This method is defined to be able to write
#
# @person.try(:name)
#
# instead of
#
# @person.name if @person
#
# +try+ calls can be chained:
#
# @person.try(:spouse).try(:name)
#
# instead of
#
# @person.spouse.name if @person && @person.spouse
#
# +try+ will also return +nil+ if the receiver does not respond to the method:
#
# @person.try(:non_existing_method) # => nil
#
# instead of
#
# @person.non_existing_method if @person.respond_to?(:non_existing_method) # => nil
#
# +try+ returns +nil+ when called on +nil+ regardless of whether it responds
# to the method:
#
# nil.try(:to_i) # => nil, rather than 0
#
# Arguments and blocks are forwarded to the method if invoked:
#
# @posts.try(:each_slice, 2) do |a, b|
# ...
# end
#
# The number of arguments in the signature must match. If the object responds
# to the method the call is attempted and +ArgumentError+ is still raised
# in case of argument mismatch.
#
# If +try+ is called without arguments it yields the receiver to a given
# block unless it is +nil+:
#
# @person.try do |p|
# ...
# end
#
# You can also call try with a block without accepting an argument, and the block
# will be instance_eval'ed instead:
#
# @person.try { upcase.truncate(50) }
#
# Please also note that +try+ is defined on +Object+. Therefore, it won't work
# with instances of classes that do not have +Object+ among their ancestors,
# like direct subclasses of +BasicObject+.
##
# :method: try!
#
# :call-seq:
# try!(*a, &b)
#
# Same as #try, but raises a +NoMethodError+ exception if the receiver is
# not +nil+ and does not implement the tried method.
#
# "a".try!(:upcase) # => "A"
# nil.try!(:upcase) # => nil
# 123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Integer
end
class Delegator
include ActiveSupport::Tryable
##
# :method: try
#
# :call-seq:
# try(a*, &b)
#
# See Object#try
##
# :method: try!
#
# :call-seq:
# try!(a*, &b)
#
# See Object#try!
end
class NilClass
# Calling +try+ on +nil+ always returns +nil+.
# It becomes especially helpful when navigating through associations that may return +nil+.
#
# nil.try(:name) # => nil
#
# Without +try+
# @person && @person.children.any? && @person.children.first.name
#
# With +try+
# @person.try(:children).try(:first).try(:name)
def try(_method_name = nil, *)
nil
end
# Calling +try!+ on +nil+ always returns +nil+.
#
# nil.try!(:name) # => nil
def try!(_method_name = nil, *)
nil
end
end